列表初始化编译错误



c++11标准8.5.4列表初始化示例,例如:

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

但我试过VC10、gcc 4.6和Comeau,这些编译器都不会让它通过?为什么?

感谢您在评论中给出的所有答案。

然后我检查了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;
}

相关内容

最新更新