Jump to content
  • Advertisement
Sign in to follow this  
Matt Green

VS8 adds for each as a nonstandard unmanaged C++ extension

This topic is 4640 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Try it! The syntax is for each(type declaration in collection). I'm not compiling with /clr at all, and it works just fine. I've been using BOOST_FOREACH for awhile now (macro'd to foreach()), but it will be very hard to avoid this in the name of portability. :)

Share this post


Link to post
Share on other sites
Advertisement
I'll be damned, it does work.


#include <vector>
#include <cstdlib>
#include <algorithm>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
vector<int> numbers;
generate_n(back_inserter(numbers), 10, rand);

for each(int i in numbers)
{
cout<<i<<endl;
}

return 0;
}




EDIT: MSDN.

Share this post


Link to post
Share on other sites

#if _MSC_VER >= 1400
#define foreach(var, container) if(false) {} else for each(var in container)
#else
#define foreach(var, container) BOOST_FOREACH(var, container)
#endif



Now it's portable.

EDIT: ...as long as you only iterate over standard containers, since Boost.Foreach also supports iterating any Boost.Range.

[Edited by - jdhardy on December 3, 2005 3:21:16 PM]

Share this post


Link to post
Share on other sites
which is weird, because we already have std::for_each. If only they'd chosen to add lambdas instead.

Share this post


Link to post
Share on other sites
Now that's just plain messed up. I mean, everyone knows there is no such thing as a for each construct in C++, so, WTF? Adding obviously non-standard things like that is, in my opinion, not a very clever thing for Microsoft to do..

Share this post


Link to post
Share on other sites
Huh, that does look intersting. Still, I'd prefer real lambdas built into the compiler, rather than brittle template hacks which produce 200-line error messages.

Share this post


Link to post
Share on other sites
Quote:
Original post by SirLuthor
Now that's just plain messed up. I mean, everyone knows there is no such thing as a for each construct in C++, so, WTF? Adding obviously non-standard things like that is, in my opinion, not a very clever thing for Microsoft to do..


It's probably just an artifact from the C++/CLI for each construct. Basically it was already in the compiler anyways.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by SirLuthor
Now that's just plain messed up. I mean, everyone knows there is no such thing as a for each construct in C++, so, WTF? Adding obviously non-standard things like that is, in my opinion, not a very clever thing for Microsoft to do..


It's probably just an artifact from the C++/CLI for each construct. Basically it was already in the compiler anyways.


Exactly. The for each, in construct was designed for C++/CLI, however in building it they also added support for arrays and standard C++ containers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!