Jump to content
  • Advertisement
Sign in to follow this  
Storyyeller

best c++ foreach macro?

This topic is 2553 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

Note: Assume hypothetically that boost foreach doesn't exist.
What is the best way to write a C++ foreach macro?

The most obvious method is something like
[source='cpp'] #define foreach(var, container) for(auto var = (container).begin(); var != (container).end(); ++var)
foreach(foo, foos) { (*foo) *= 3; } [/source]

However, this forces you to dereference the pointer/iterator. It's usually more convenient to have a reference. One possibility I thought of is
[source='cpp'] #define foreach(var, container) for(auto iter = (container).begin(); iter != (container).end(); ++iter) {auto& var = *iter;
foreach(foo, foos) foo *= 3; } [/source]

The main problem with this is that the missing { completely mangles IDE code folding.

Share this post


Link to post
Share on other sites
Advertisement
Use of "auto"? Use lambdas:


std::for_each( container.begin(), container.end(),
[&] ( const container_type::value_type& val ) {

}
);

Share this post


Link to post
Share on other sites

Use of "auto"? Use lambdas:


If you have a compiler whose support of C++11 is far along enough to support auto and lambdas, chances are that it supports C++11's foreach syntax anyway:


for( auto& val : container )
{
}


gcc 4.6 supports this, I'm not sure about visual studio 2010. It was just added in clang too iirc.

Share this post


Link to post
Share on other sites

gcc 4.6 supports this, I'm not sure about visual studio 2010. It was just added in clang too iirc.

VS10 does support a for each construct, but not the standard one.
[source]
for each(auto &val in container)
{
}
[/source]

Share this post


Link to post
Share on other sites

[quote name='Zlodo' timestamp='1311237640' post='4838351']
gcc 4.6 supports this, I'm not sure about visual studio 2010. It was just added in clang too iirc.

VS10 does support a for each construct, but not the standard one.
[source]
for each(auto &val in container)
{
}
[/source]
[/quote]

I thought that was the C++/CLI syntax. Does it work with native C++ code?

Share this post


Link to post
Share on other sites

[quote name='Brother Bob' timestamp='1311240100' post='4838361']
[quote name='Zlodo' timestamp='1311237640' post='4838351']
gcc 4.6 supports this, I'm not sure about visual studio 2010. It was just added in clang too iirc.

VS10 does support a for each construct, but not the standard one.
[source]
for each(auto &val in container)
{
}
[/source]
[/quote]

I thought that was the C++/CLI syntax. Does it work with native C++ code?
[/quote]
Yes, it does. I don't know the details or limitations with it, or how it compares to the standard syntax, but it does work for iterating over all values in a container supporting the begin/end semantics at least.

Share this post


Link to post
Share on other sites

[quote name='fastcall22' timestamp='1311226132' post='4838316']
Use of "auto"? Use lambdas:


If you have a compiler whose support of C++11 is far along enough to support auto and lambdas, chances are that it supports C++11's foreach syntax anyway:
[/quote]

Not necessarily. GCC supported auto as early as 4.4, but didn't support ranged based for until 4.6

Share this post


Link to post
Share on other sites
Not necessarily. GCC supported auto as early as 4.4, but didn't support ranged based for until 4.6

Which is why I said "auto AND lambdas", my reasoning being that lambdas are more complex to implement than the for each construct and thus it is likely that by the time they get around to implement it they already did the foreach construct (which is the case with clang, for instance).

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!