Although I'm more than knowledgeable about C and OOP in Java, I'm starting to dive into C++ and its particularities. I've read all the basic things about C++, but I'm still confused with some C++11-specific things, both syntax- and performance-wise. On of such things are container iterators, which I have found implemented in a myriad of syntax forms (e.g. range-based loops).
I was wondering which of these are completely equivalent, why would one want to use one or another and what are the effects in performance.
a)auto vs explicit declaration:
Is the auto always supported? Aside from code readability issues, why would a programmer prefer the explicit declaration?
list<int>::const_iterator i = myIntList.begin(); /* Option a1 */
auto i = myIntList.begin(); /* Option a2 */
for(auto i : myIntList) { ... } /* Option a3 */
for(int i : myIntList) { ... } /* Option a4 */
b) Compact form vs extended loop form
list<int> l = {1, 2, 3, ...};
for(auto i : l) { ... } /* Option b1 */
for(auto i = l.begin(); i != l.end(); ++i) { ... } /* Option b2 */
c) Constant, non-constant / Access type
Why/when would one prefer to have a reference or a constant in the loop body?
/* Constant/non-constant: */
for(list<int>iterator i = l.begin(); ...) { ... } /* Option c1 */
for(list<int>const_iterator i = l.begin(); ...) { } /* Option c2 */
for(const int& i : list) { ... } /* Option c3 */
for(int& i : list) { ... } /* Option c4 */
/* Access by reference/by value: */
for(auto&& i : list) { ... } /* Option c5 */
for(auto i : list) { ... } /* Option c6 */
d) Loop's exit condition:
/* Option d1: end is defined within the start condition or outside the loop. */
for(auto i = l.begin(), end = l.end(); i != end; ++i) { ... }
/* Option d2: end is defined in the continue condition. */
for(auto i = l.begin(); i != l.end(); ++i) { ... }
Perhaps most of them are identical, and perhaps the choice of one option or another only makes sense for a given loop body, but I wonder what's the purpose of allowing so many possible ways of programming the same behaviour.