Visual c++ "for each" portability

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.


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]


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();