Is it possible to use a list of initializers enclosed in parentheses for a container container?

I understand that from C ++ 11 I can initialize a container using a list of initializers enclosed in brackets:

std::map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}}; 

Is this also possible for container containers?

For example, I tried the following without success:

 std::pair<std::map<int, char>, int> a = {{1, 'c'}, 2}; 

In Visual Studio 2015, I get the following compilation error:

no constructor instance "std :: map <_Kty, _Ty, _Pr, _Alloc> :: map [with _Kty = std :: map, std :: allocator →, _Ty = int, _Pr = std :: less, std :: allocator →>, _Alloc = std :: allocator, std :: allocator →, int →] "matches the list of arguments Types of arguments: ({...}, int)

With MinGW32, a compilation error was in the lines

Failed to convert {...} from the list of initializers enclosed in brackets to std :: pair ...

+5
source share
1 answer

You do not have brackets for your map (and "c" must be 'c' , because "c" is const char * not a char , thanks to Bastien Durel):

 std::pair<std::map<int, char>, int> a = {{{1, 'c'}}, 2}; 

To use the initializer list to initialize the map, you need a "list of pairs", something like {{key1, value1}, {key2, value2}, ...} . If you want to pair this, you need to add another level of parentheses that give {{{key1, value1}, {key2, value2}, ...}, second} .

+8
source

All Articles