Visual c++“为每个"可移植性

2021-12-22 00:00:00 visual-c++ foreach c++ stl

我最近才发现 Visual C++ 2008(也许还有更早的版本?)支持 stl 列表等上的 for each 语法以促进迭代.例如:

I only just recently discovered that Visual C++ 2008 (and perhaps earlier versions as well?) supports for each syntax on stl lists et al to facilitate iteration. For example:

list<Object> myList;

for each (Object o in myList)
{
  o.foo();
}

我很高兴发现它,但是当有人决定我需要能够在 gcc 或其他一些编译器中编译我的代码时,我担心可怕的一天的可移植性.这种语法是否得到广泛支持,我可以使用它而不必担心可移植性问题吗?

I was very happy to discover it, but I'm concerned about portability for the dreaded day when someone decides I need to be able to compile my code in say, gcc or some other compiler. Is this syntax widely supported and can I use it without worrying about portability issues?

推荐答案

对于每个不是标准的 C 或 C++ 语法.如果您希望能够在 gcc 或 g++ 中编译此代码,则需要创建一个迭代器并使用标准 for 循环.

For each is not standard C or C++ syntax. If you want to be able to compile this code in gcc or g++, you will need to create an iterator and use a standard for loop.

量子皮特

这似乎是 MS Visual C++ 中引入的一项新功能,因此这绝对是不可移植的.参考:http://msdn.microsoft.com/en-us/library/xey702bw%28VS.80%29.aspx [/edit]

[edit] This seems to be a new feature introduced into MS Visual C++, so this is definitely not portable. Ref: http://msdn.microsoft.com/en-us/library/xey702bw%28VS.80%29.aspx [/edit]

相关文章