Visual c++“为每个"可移植性 [英] Visual c++ "for each" portability

查看:26
本文介绍了Visual c++“为每个"可移植性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近才发现 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]

这篇关于Visual c++“为每个"可移植性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆