The reason it doesn't work is because 0 doesn't match the type of v.size(). Worse, v.size() is an unsigned type while the literal 0 is a signed integer. At most there will be a warning, but that's still annoying, so I set out to come up with a better option that's consistent across the board and has a more conventional syntax.

Option 1: decltype

C++11 also offers the decltype keyword for deducing the type of an expression or entity. In this case we can deduce the type of v.size() and use that for the type of i. Everything works nicely with perfect type matching and no warnings, but it's kind of ugly syntax-wise:

Option 2: Iterator arithmetic

An option that exists prior to C++11 is by using iterator subtraction to find the current index. It's effective and conventional in terms of loop syntax, but the arithmetic feels wrong to my eyes. Maybe I'm mentally compiling it into slower code than a more direct loop over an index:

Also note that if you don't need the current index, the iterator can be used to acquire the value of the current item: cout << *x << ' ';. But if you don't need the current index, a range based for loop may be the superior option in the first place.

Option 3: ibegin() and iend()

This is the solution I came up with (see the snippet below for the implementation). The idea is to provide an index-based version of the begin() and end() non-member functions that return an iterator:

Notice how now the assumption is only that v is a container with an implementation of ibegin()/iend() and an overloaded subscript operator. Like with begin()/end(), this means v could be an array or a standard container, or even a custom container that provides suitable overloads.

In the case of the non-array overloads, the only assumption is that the container supports a member function called size() which returns a value of the index type. If that's not the case for user-defined container classes, ibegin() and iend() can be overloaded as necessary to meet the needs of the class (just like the standard begin() and end().