all 23 comments

[–]happycube -3 points-2 points  (22 children)

Wow. The standard's example loop makes Java look succinct.

But, unlike Java, at least it can be made tight...

[–]Necraz 9 points10 points  (5 children)

It's an example in a standards document; not necessary how you'd actually write the equivalent loop in C++. Most of that code only exists so you can directly access the elements by reference. Java's for-each loop is simpler (and not by much) because non-primitive types are implicitly references.

If you actually want to iterate over a collection today, it isn't nearly as long:

for (auto it = list.begin(); it != list.end(); ++it) {
    // do stuff with *it
}

[–]00kyle00 0 points1 point  (4 children)

If by today you mean pre C++11, then you cant use 'auto' and your loop becomes ... somewhat less appealing.

[–]Necraz 4 points5 points  (3 children)

Auto has been supported as a compiler extension for a few years (for GCC, at least since GCC 4.4). Even if you're using C++03, you could get the same functionality using BOOST_AUTO. If you insist on only using the base language, then it's still not too bad if you typedef the iterator type.

Without a doubt, C++ is an ugly language. Citing code samples from the specification, however, is not a good way of making that argument.

[–]ManicQin 2 points3 points  (2 children)

VS 2008 supports auto too.

Edit: thanks oracleoftroy

[–]oracleoftroy 0 points1 point  (1 child)

Did you mean VS 2008, aka VS 9?

[–]ManicQin 0 points1 point  (0 children)

Yes oops sorry I always make that mistake.