新的C ++ 11 range-for(foreach)语法:哪些编译器支持它?

时间:2011-02-17 17:34:35

标签: c++ for-loop foreach c++11

我在this BoostCon presentation by Jeremy Siek中看到了这个c ++ 11代码片段:

deque<int> topo_order;
topological_sort(g, front_inserter(topo_order));

for (int v : topo_order){ //line 39
    cout << tasks[v] << endl;
}

尝试在gcc中编译时出现以下错误:

main.cpp:39: error: expected initializer before ‘:’ token

然后让我想知道,哪些编译器实际上支持这种语法?

2 个答案:

答案 0 :(得分:7)

好吧,至少GCC supports it in 4.6(功能称为“基于范围”)。如果您已经拥有最新版本,请不要忘记添加-std=c++0x选项。

答案 1 :(得分:1)

除了版本4.6之后的gcc版本,Clang 3.0和Visual C ++ 11(从Visual C ++ 11 Beta开始)都支持这种C ++ 11语言功能。