2012-02-06 147 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