auto specifier (commonly referred to as a keyword) was introduced in C++11. It allows the programmer to tell the compiler to work out the type of the variable, rather than the programmer having to specify it in the code.
The compiler works out what the type of a auto variable should be from it’s initializer (the right-hand side the equals sign). For the example above, the compiler determines the type of x as an integer, as this is the default type for an integer value.
auto can be used as the return type for functions, both for traditional functions and lambdas.
For the above function
Foo(), the compiler deduces the return type as
int, as this is again the default type for an integer.
In the below example, the auto return type saves much typing/code mess!
Although they are similar and often used in the same lines of code, auto should not be confused with
auto operates on types, while
decltype operates on expressions.
This work is licensed under a Creative Commons Attribution 4.0 International License .