3

Currently in a large c++ project we are working on we have a bunch of the new style for loops like the following:

for (auto& value : values)

Up till now we have been compiling exclusively with gcc 4.6. Recently some of the codebase is being ported to windows and some of the developers want to compile in msvc++ 10 but it seems as though the new for loop syntax is not fully supported yet.

It would be highly desirable to not have to re-write all the places where this syntax occurs.

What is the best way to work around this problem?

Update: It looks as though this issue is resolved in MSVC11.

2
  • There's no way you're going to avoid some rewriting. VC++10 simply doesn't support it. And the only reason VC++11 will is because of the tireless efforts of the compiler writers, taking weekend time out to slip it into the build. Commented Feb 10, 2012 at 4:20
  • @NicolBolas: wow, I didn't know VC++ dev team was so devoted to their work. It's even more the pity that they are not further backed by Microsoft :x (as in: getting more devs) Commented Feb 10, 2012 at 7:10

4 Answers 4

7

You could use Boost.Foreach:

//Using Xeo's example:
BOOST_FOREACH (auto& e, values) {
    std::cout << e << " ";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Note that you can actually use auto here. :)
@Xeo: Of course... I was sitting in C++03 land for a bit there (;
3

One way would be to replace them with std::for_each and lambdas, where possible. GCC 4.6 and MSVC10 both support lambda expressions.

// before:
for(auto& e : values){
  std::cout << e << " ";
}

// after:
std::for_each(values.begin(), values.end(),
    [](a_type& e){
      std::cout << e << " ";
    });

The only caveat is that you need to actually type the element name (a_type here), and that you can't use control flow structures (break, continue, return).

Another way would be, when you need those control flow structures, to use old-style for-loops. Nothing wrong with them, especially with auto to infer the iterator type.

Yet another way might be to use the Visual Studio 11 beta when it's out, it supports range-based for loops IIRC. :)

17 Comments

Interesting idea, but the lack of control structures is definitely an annoyance.
"Yet another way might be to use the Visual Studio 11 beta when it's out, it supports range-based for loops IIRC." Nope, not according to this blog post.
@ildjarn: Yes, according to STL himself at his GoingNative 2012 panel. :) (Starting at somewhere around 38 minutes.)
Hmm, I haven't watched that yet. Are you sure he wasn't referring to VC++'s proprietary for each, in construct? Because the current VC++11 docs don't reflect support for proper C++11 range-based for yet. EDIT: Just watched it (1:39 in), you're totally correct -- (good) news to me. :-D
@ildjarn The blog post has been updated to say that range-based for loops will be in VC11 Beta.
|
2

You don't have to use boost.
Here's a simple macro for backwards compatibility with vs2010:

// 1600 is VS2010
#if _MSC_VER == 1600
    #define FOR_EACH(var , range) for each(var in range)
#else
    #define FOR_EACH(var , range) for (var : range)
#endif

Use it like this:

FOR_EACH(auto& e, values) {
    std::cout << e << " ";
}

Comments

0

You can use for each itself

for each (auto value in values) {
std::cout << value << endl;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.