C++ compile error using initializer list on map<string, string>

92 Views Asked by At

Given this class which takes a map as a constructor parameter

struct Map
{
    Map(const map<string, string>& map)
    {
    }
};

And this sample code to create it

string s;
Map j({ {s, s}});

I get:

Error   C2675   unary '++': '_Iter' does not define this operator or a conversion to a type acceptable to the predefined operator   BioAgent    C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.36.32532\include\xtree  1259    

If I change the map to map<string, int> and create it with { { s, 1} } there is no error.

If I write a test function, with the same parameter as the constructor:

static void Test(const map<string, string>& map)
{
}

And call:

Test({ {s, s} });

It also works fine.

Why is only the constructor failing to implicitly convert the initializer list to the map?

0

There are 0 best solutions below