2012-02-06 150 views
6

C++ 11标准8.5.4例如,对于清单initializtion说:列表初始化编译错误

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

但我已经试过VC10,GCC 4.6和科莫,没有这些编译器会让这个通行证?这是为什么 ?

+4

的C++ 11标准很新,不是所有的编译器和库支持一切呢。 – 2012-02-06 10:13:30

+6

如果指定'-std = C++ 0x',GCC 4?5,4.6和4.7会编译。 – Mat 2012-02-06 10:13:55

+0

[Works](http://ideone.com/37oqu)为我在gcc 4.5.1上。你得到了什么错误?也许你还没有启用'-std = C++ 0x'? – 2012-02-06 10:14:20

回答

3

感谢评论中的所有答案。

然后我检查了C++ 98和03标准,并且8.5.4肯定是C++ 11中的新的第二个! 这就是为什么它没有得到所有编译器的全面支持。

使用gcc 4.6.1添加flag -std = C++ 0x现在编译好了。

添加测试代码的任何东西谁可能需要一个参考:

#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; 
} 
+0

@TomGarske:谢谢你的提醒,只是做:) – Gob00st 2012-06-14 22:30:40