List initialization error

C ++ 11 standard 8.5.4 example to initialize a list:

std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} }; 

But I tried VC10, gcc 4.6 and Comeau, none of these compilers allowed this? Why is this?

+6
source share
1 answer

Thanks for all the answers in the comments.

Then I checked back the C ++ 98 and 03 standard, and yes, 8.5.4 is definitely a new second in C ++ 11! This is why it is not fully supported by all compilers.

After adding the -std = C ++ 0x flag with gcc 4.6.1 now it compiles fine.

Adding test code for anyone who might need a link:

 #include <map> #include <string> #include <initializer_list> #include <iostream> using namespace std; int main() { std::map<std::string,int> collection = {{"bear",4}, {"cassowary",2}, {"tiger",7}}; for(auto it: collection) std::cout << it.first << " has value " << it.second << std::endl; return 0; } 
+3
source

Source: https://habr.com/ru/post/907758/


All Articles