New C++11 range-for (foreach) syntax: which compil

2020-07-05 10:45发布

I saw this c++11 code fragment in this BoostCon presentation by Jeremy Siek:

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

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

Upon trying to compile in gcc there is the following error:

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

which then got me wondering, which compilers actually support this syntax?

2条回答
干净又极端
2楼-- · 2020-07-05 11:02

Well, at least GCC supports it in 4.6 (feature is called "Range-based for"). If you already have the latest version, don't forget to add the -std=c++0x option.

查看更多
可以哭但决不认输i
3楼-- · 2020-07-05 11:23

In addition to gcc versions later than version 4.6, both Clang 3.0 and Visual C++ 11 (as of the Visual C++ 11 Beta) both support this C++11 language feature.

查看更多
登录 后发表回答