Using auto whenever possible
Automatic type deduction is one of the most important and widely used features in modern C++. The new C++ standards have made it possible to use auto
as a placeholder for types in various contexts and let the compiler deduce the actual type. In C++11, auto
can be used for declaring local variables and for the return type of a function with a trailing return type. In C++14, auto
can be used for the return type of a function without specifying a trailing type and for parameter declarations in lambda expressions. Future standard versions are likely to expand the use of auto
to even more cases. The use of auto
in these contexts has several important benefits. Developers should be aware of them, and prefer auto
whenever possible. An actual term was coined for this by Andrei Alexandrescu and promoted by Herb Sutter--almost always auto (AAA).
How to do it...
Consider using auto
as a placeholder for the actual type in the following situations:
- To declare local variables...