best c++ foreach macro?

Started by
16 comments, last by Brother Bob 12 years, 8 months ago
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.
I trust exceptions about as far as I can throw them.
Advertisement
Use of "auto"? Use lambdas:


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

}
);
Turns out to be pretty difficult
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

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.

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 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?
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

[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.

[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
I trust exceptions about as far as I can throw them.
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).
Also, is it possible to continue or break with lambdas?
I trust exceptions about as far as I can throw them.

This topic is closed to new replies.

Advertisement