views:

251

answers:

3

Visual C++ 2010 accepts:

std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
for each (auto i in v)
    std::cout << i << std::endl;

Is this a C++0x feature or a Microsoft extension? According to Wikipedia, the syntax of C++0x's for-each loop different:

int myint[] = {1,2,3,4,5};
for (int& i: myint)
{
    std::cout << i;
}
+8  A: 

The current standard draft does not include the for each ( auto i in v ) syntax, only the for ( auto i : myints ), so yes, it is just an extension.

David Rodríguez - dribeas
+3  A: 

Besides of the for loops version presented above a c++0x standard conformal version would also be:

std::for_each (v.begin(), v.end(), [] (int)->void { 
    std::cout << i << std::endl;
});

The construct you have presented, is also standard conformal, but it conforms to different standard: The ECMA C++/CLI specification.

paul_71
If it is as he said, that it compiles to native code, wouldn't it be C++ instead of C++/Cli? Or is he wrong? Or am I wrong?
Johannes Schaub - litb
Technically, "C++0x standard" is an oxymoron. It's not a standard yet. (And when it becomes one, it'll no longer be called C++0x) ;)
jalf
@jalf Well, since we do not have a better name for it yet, I think that one is the best available... @Johannes No, this always compiles to IL, or to that mysterious mixture of IL and native code. This syntax may only work, if you compile with CLR support (/clr option).
paul_71
I have to correct my comment from above. With vc9 and vc10 this syntax compiles in native code too. This should not be, the /clr option should be required for that.
paul_71
@paul_71: I know, just being pedantic. ;) (although I'd say "C++0x conformant", without the "standard". Or just "a C++0x version"
jalf
+3  A: 

In VS2010, for any such doubt, try with the /Za flag under "C/C++->Language" in the IDE project settings.

As for your query, yes, this is not standard C++ syntax.

Chubsdad
Indeed, with /Za I get an compiler error at the loop.
George