视觉c++ “对于每个” 可移植性

发布于 2024-07-07 08:01:19 字数 286 浏览 6 评论 0原文

我最近才发现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

冰之心 2024-07-14 08:01:20

我不会用那个。 虽然这是一个诱人的功能,但其语法与即将推出的 C++0x 标准不兼容,该标准使用:

list<Object> myList;

for (Object o : myList)
{
   o.foo();
}

来做同样的事情。

I wouldn't use that. While it's a tempting feature, the syntax is incompatible with the upcoming C++0x standard, which uses:

list<Object> myList;

for (Object o : myList)
{
   o.foo();
}

to do the same thing.

最冷一天 2024-07-14 08:01:20

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]

丑疤怪 2024-07-14 08:01:20

有一个非常好的便携式替代方案: Boost.Foreach< /a>. 只需将此标头转储到您的项目中,您就可以按如下方式编写循环:

list<Object> myList;

BOOST_FOREACH(Object o, myList)
    o.foo();

There is a very good portable alternative: Boost.Foreach. Just dump this header into your project and you can write your loops as follows:

list<Object> myList;

BOOST_FOREACH(Object o, myList)
    o.foo();
随梦而飞# 2024-07-14 08:01:20

如果您想使用 foreach 并且同时不想添加额外的依赖项(例如 Boost) - 这个宏将帮助您:

#define VAR(V,init) __typeof(init) V=(init)
#define FOREACH(I,C) for(VAR(I,(C).begin());I!=(C).end();I++)

std::vector<int> numbers;

FOREACH(I, numbers)
{
    std::cout << *I << std::endl;
}

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:

#define VAR(V,init) __typeof(init) V=(init)
#define FOREACH(I,C) for(VAR(I,(C).begin());I!=(C).end();I++)

std::vector<int> numbers;

FOREACH(I, numbers)
{
    std::cout << *I << std::endl;
}
深爱不及久伴 2024-07-14 08:01:20

“foreach”的 Visual C++ 不是标准 C++,这意味着您无法在其他编译器(例如 g++)上编译代码。 不过STL提出了std::for_each,但是它的语法很多不太直观。 这是它的原型:

template <class InputIterator, class UnaryFunction>
UnaryFunction for_each(InputIterator first, InputIterator last, UnaryFunction f);

它需要两个定义有效范围的迭代器,并将一元函数(或函子)f 应用于该范围内的每个对象。
您可以使用 std::for_each 重写您的示例,如下所示:

void foo(Object o)
{
  o.foo();
}
...
list<Object> myList;

std::for_each(myList.begin(), myList.end(), foo);

但是,如果您想接近 foreach 构造的经典语法,并且您可以使用 Boost,则可以使用 BOOST.FOREACH,这将让你写

list<Object> myList;

BOOST_FOREACH(Object o, myList)
{
    o.foo();
}

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:

template <class InputIterator, class UnaryFunction>
UnaryFunction for_each(InputIterator first, InputIterator last, UnaryFunction f);

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:

void foo(Object o)
{
  o.foo();
}
...
list<Object> myList;

std::for_each(myList.begin(), myList.end(), foo);

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

list<Object> myList;

BOOST_FOREACH(Object o, myList)
{
    o.foo();
}
删除会话 2024-07-14 08:01:20

Boost 库有一个可移植的 ForEach 实现

The Boost Library has a portable ForEach imlementation.

许仙没带伞 2024-07-14 08:01:20

你的代码确实不可移植。

以下适用于 C++ 0x 标准和 Visual C++ 2010(据我所知,它不支持新的“ranged for”语法)。

#define for_each(_ITER_, _COLL_) for (auto _ITER_ = _COLL_.begin(); \
    _ITER_ != _COLL_.end(); _ITER_++)

现在您可以编写:

list<Object> myList;

for_each (o, myList)
{
  o.foo();
}

将其与 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).

#define for_each(_ITER_, _COLL_) for (auto _ITER_ = _COLL_.begin(); \
    _ITER_ != _COLL_.end(); _ITER_++)

Now you can write:

list<Object> myList;

for_each (o, myList)
{
  o.foo();
}

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.

甜扑 2024-07-14 08:01:20

我还推荐 BOOST_FOREACH。 我通常按​​照以下方式创建宏:

#define _foreach(x,y) BOOST_FOREACH(x,y)

这往往会增加可读性。 不过,您必须小心与其他 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:

#define _foreach(x,y) BOOST_FOREACH(x,y)

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.

樱&纷飞 2024-07-14 08:01:20

我投票给 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文