CppCoreGuidelines advise the following:
ES.11: Use auto
to avoid redundant repetition of type names
Reason
- Simple repetition is tedious and error-prone.
- When you use
auto
, the name of the declared entity is in a fixed position in the declaration, increasing readability.
- In a function template declaration the return type can be a member type.
Example
Consider:
auto p = v.begin(); // vector<int>::iterator
auto h = t.future();
auto q = make_unique<int[]>(s);
auto f = [](int x) { return x + 10; };
In each case, we save writing a longish, hard-to-remember type that the compiler already knows but a programmer could get wrong.
Example
template<class T>
auto Container<T>::first() -> Iterator; // Container<T>::Iterator
Exception
Avoid auto
for initializer lists and in cases where you know exactly which type you want and where an initializer might require conversion.
Example
auto lst = { 1, 2, 3 }; // lst is an initializer list
auto x{1}; // x is an int (in C++17; initializer_list in C++11)
Note
When concepts become available, we can (and should) be more specific about the type we are deducing:
// ...
ForwardIterator p = algo(x, y, z);
Example (C++17)
auto [ quotient, remainder ] = div(123456, 73); // break out the members of the div_t result
Enforcement
Flag redundant repetition of type names in a declaration.
The Urho3D coding conventions used to have a link to these guidelines, but it seems to have disappeared.