视觉c++ “对于每个” 可移植性
我最近才发现 Visual C++ 2008(也许还有早期版本?)支持 stl 列表等上的 foreach
语法以方便迭代。 例如:
list<Object> myList;
for each (Object o in myList)
{
o.foo();
}
我很高兴发现它,但我担心可怕的一天的可移植性,当有人决定我需要能够在 gcc 或其他编译器中编译我的代码时。 这种语法是否得到广泛支持?我可以使用它而不用担心可移植性问题吗?
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();
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我不会用那个。 虽然这是一个诱人的功能,但其语法与即将推出的 C++0x 标准不兼容,该标准使用:
来做同样的事情。
I wouldn't use that. While it's a tempting feature, the syntax is incompatible with the upcoming C++0x standard, which uses:
to do the same thing.
For every 不是标准 C 或 C++ 语法。 如果您希望能够在 gcc 或 g++ 中编译此代码,则需要创建一个迭代器并使用标准 for 循环。
量子皮特
[编辑]
这似乎是 MS Visual C++ 中引入的新功能,因此绝对不可移植。 参考: http://msdn.microsoft.com/en -us/library/xey702bw%28VS.80%29.aspx [/编辑]
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.
QuantumPete
[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]
有一个非常好的便携式替代方案: Boost.Foreach< /a>. 只需将此标头转储到您的项目中,您就可以按如下方式编写循环:
There is a very good portable alternative: Boost.Foreach. Just dump this header into your project and you can write your loops as follows:
如果您想使用 foreach 并且同时不想添加额外的依赖项(例如 Boost) - 这个宏将帮助您:
If you'd like to use foreach and in the same time you don't want to add additional dependency (such as Boost) - this macro will help you:
“foreach”的 Visual C++ 不是标准 C++,这意味着您无法在其他编译器(例如 g++)上编译代码。 不过STL提出了std::for_each,但是它的语法很多不太直观。 这是它的原型:
它需要两个定义有效范围的迭代器,并将一元函数(或函子)f 应用于该范围内的每个对象。
您可以使用 std::for_each 重写您的示例,如下所示:
但是,如果您想接近 foreach 构造的经典语法,并且您可以使用 Boost,则可以使用 BOOST.FOREACH,这将让你写
Visual C++ "for each" is not standard C++, meaning you won't be able to compile your code on other compilers such as g++. However, the STL proposes std::for_each, but its syntax is a lot less intuitive. Here is its prototype:
It takes two iterators defining a valid range, and applies the unary function (or functor) f to each object in this range.
You can rewrite your example using std::for_each like this:
However, if you want to stay close to the classical syntax of the for each construct, and if you're ok about using Boost, you can use BOOST.FOREACH, which will let you write
Boost 库有一个可移植的 ForEach 实现 。
The Boost Library has a portable ForEach imlementation.
你的代码确实不可移植。
以下适用于 C++ 0x 标准和 Visual C++ 2010(据我所知,它不支持新的“ranged for”语法)。
现在您可以编写:
将其与 http:// 中的 BOOST_FOREACH 宏代码进行比较www.boost.org/doc/libs/1_48_0/boost/foreach.hpp 这不仅很复杂,而且还对其他 boost 库有许多依赖项。
Your code is indeed not portable.
The following works with the C++ 0x standard and Visual C++ 2010 (which doesn't support the new "ranged for" syntax as far as I can tell).
Now you can write:
Compare this to the BOOST_FOREACH macro code at http://www.boost.org/doc/libs/1_48_0/boost/foreach.hpp which is not only complex it also has a number of dependencies on other boost libraries.
我还推荐 BOOST_FOREACH。 我通常按照以下方式创建宏:
这往往会增加可读性。 不过,您必须小心与其他 foreach 实现的冲突。 例如,Qt 提供了“foreach”,还有 std::for_each。
我发现 std::for_each 实际上并没有节省太多时间,因为您最终会创建大量一次性函数对象来提供给 for_each 调用。 使用 STL 迭代器创建标准 for 循环通常同样快。
I also recommend BOOST_FOREACH. I usually create a macro along the lines of:
This tends to increase readability. You have to be careful about collisions with other foreach implementations though. For instance, Qt provides a 'foreach' and there's the std::for_each.
I find that the std::for_each doesn't actually save much time since you end up making lots of one-off function objects to supply to the for_each call. It's usually just as fast to make standard for-loop using STL iterators.
我投票给 Luc,
坚持使用标准 STL 算法,到目前为止你的情况会更好。 STL算法可以让您的生活变得非常轻松、高效和安全。 看一下现成的算法,如 find_if、count、count_if、sort、transform 等...
第 5 点开始...
http://www.sgi.com/tech/stl/table_of_contents.html
Boost 很酷,但是如果您打算仅将它用于 FOR_EACH 宏,那么开发/构建环境设置就太麻烦了。
当标准 c++ / stl 无法以“简单”方式解决问题时,请使用 boost。
My vote goes for Luc,
Stick to the standard STL algorithms and you will be better off by far. STL algorithms can make your life very easy, efficient and safe. Take a look at the off the shelve algorithms like find_if, count, count_if, sort, transform, etc...
Point 5 onwards...
http://www.sgi.com/tech/stl/table_of_contents.html
Boost is cool, but if you are going to use it just for the FOR_EACH macro it is too much cumbersome regarding the development/build environment setup.
use boost when standard c++ / stl cannot solve the problem in an 'easy' way.