The Last Word on auto (For Now)
auto has meandered about a bit, so I'd like to point out what I think are the important themes.
The discussion about auto has meandered about a bit (see previous posts here and here), so I'd like to point out what I think are the important themes before moving on to other topics.
When I write
auto var = expression;
I am saying that I want var to have the same type as expression. The main objection that readers have raised to this technique is that we generally can't tell by looking at the code what type var has. This lack of knowledge makes programs hard to understand, so programmers should say explicitly what the type is. I have responded to that objection in several ways:
1. If you say explicitly what type var has, you now know its type; but you no longer know that var has the same type as the expression. So, for example, if I write
int N = 100000;
I know that N has type int, but I don't know that N has the same type as 100000. Even in this seemingly simple example, this lack of knowledge can lead to serious trouble. For example, I might be running on an implementation in which int cannot contain the value 100000, so that 100000 has type long. If I had written
auto N = 100000;
then I would have been assured that N is indeed 100000; but stating the type as int would yield undefined behavior.
2. auto is far from the only context in which you don't know what type a variable has when you write your code. For example:
template<class T> void f(T t) { /* … */ }
Here, t could have just about any type; there's no way to tell until you compile the program whether that type makes sense in its context.
3. Not only do you not always know what types you're using, but it can be hard to figure out how to write those types. For example, if c is a container, I might write
auto b = c.begin();
If I'm not supposed to use auto, how do I figure out what type to define for b? If I knew that c had type C, I could write
C::iterator b = c.begin();
However, this technique works only because containers follow the convention of having type members named iterator that describe the appropriate iterator types. If we are dealing with a user-defined container-like class that does not follow these conventions, we would have to write something like
decltype(c.begin()) b = c.begin();
which, as far as I am concerned, has no advantage at all over using auto.
In other words, there are many circumstances in which it makes sense to use auto — even if we ignore the question of readability. But readability is a slippery concept. People are excellent pattern matchers, and when they've seen particular patterns in use for a while, they become familiar with them. Until then, those patterns can be hard to understand. For example:
*p++ = *q++;
If you are at all serious about programming in C or C++, then at some point you spent some time figuring out what this statement could possibly mean and how it works. Later, this usage became familiar, and you no longer had any trouble understanding it. More generally, I think that when people say that a piece of code is hard to understand, what they really mean is that they don't understand it. This lack of understanding could come about for many reasons, not all of which have to do entirely with the code. In particular, the code may become easier to understand with time.
For these reasons, I think that programmers should approach auto in the same way that they approach other language features or programming techniques, namely by asking whether auto is the most straightforward way of solving the problem at hand. If it's unfamiliar, one may have to think about its meaning compared to its alternatives; but if the meaning makes sense, I think it is better to become familiar with it than to reject it on the basis of unfamiliarity alone.

