列表初始化编译错误

Gob*_*0st 6 c++ c++11

用于列表初始化的c ++ 11标准8.5.4示例说:

std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
Run Code Online (Sandbox Code Playgroud)

但我已经尝试过VC10,gcc 4.6和Comeau,那些编译器都没有让这个通过?这是为什么 ?

Gob*_*0st 3

感谢评论中的所有答案。

然后我检查了 c++ 98 和 03 标准,是的,8.5.4 绝对是 c++ 11 中的新秒!这就是为什么它没有得到所有编译器的完全支持。

使用 gcc 4.6.1 添加标志 -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;
}
Run Code Online (Sandbox Code Playgroud)