My last post about why you might not want to use auto may have left some people thinking that I think you shouldn’t use it. In fact I think you should almost always use it, as suggested in http://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/ However, like everything in C++, you should know what you are doing, because if you don’t, when you shoot yourself in the foot, you will probably blow your whole leg off.

You should read the above article, because it is a comprehensive discussion of why you might or might not want to use auto, and why you should use it all the time. Here I will add some points to the discussion about using it so that you don’t get yourself into trouble.

So now, I will go through every point in my last post, and then some.

const iterators

You should always use cbegin and cend when you are accessing a container in a way that will not change the container, period. By using the right functions you are documenting your code, and protecting yourself from accidentally changing the container. On top of that, there might be some optimisation that the compiler can make because everything is constant.

What size type do you want

Well there is no problem here if you know the standard, and use type suffixes appropriately. If you want an unsigned integer than you should write 10u . But then if you want specific size types you should probably construct them using the appropriate type. For example you could write

auto x = uint64_t{10} ; auto y = int32_t{15} ;

and then x and y would be the right size.

What are my types?

Who cares what the types are? You should be coding to interfaces rather than exact types. Then, as long as what you write satisfies the appropriate interface, it doesn’t actually matter what type it is.

What is my return type

Again, if you must use this feature, you have to be careful and know how things work so that you return the right type of object. Then, if you need to copy and paste big expressions, then again, be careful. This problem is mitigated with C++14, since you don’t have to specify a return type at all if it can be deduced from the body of the function.

To reference, or not to reference?

Well that’s easy. It’s just like declaring any other variable. You don’t get a reference unless you ask for it. But again, you have to know the rules.

Other thoughts

Of course sometimes you need to write types down. Sometimes you actually need to allocate objects of a known type on the heap. But then, you should probably be using a smart pointer, and in that case you can still only write the type once, with the following

auto p = std::make_shared<T>(init, ...);

where T is the type you want, and init, ... is the arguments used to initialise the object. This way, you don’t even have to write new . In general, the idea here is that even when you do want to specify types, you only need to write the type down once, not several times as is common in older code.

In conclusion, there really seems to be no reason why you shouldn’t almost always use auto.