For variables, indicates that the type of the declared variable will be automatically inferred from its initializer. For functions, indicates that the return type is the final return type or will be inferred from its return statements (starting with C ++ 14).
Syntax
auto variable initializer (1) (since C++11) auto function -> return type (2) (since C++11) auto function (3) (since C++14) decltype(auto) variable initializer (4) (since C++14) decltype(auto) function (5) (since C++14) auto :: (6) (concepts TS) cv(optional) auto ref(optional) parameter (7) (since C++14)
explanation
1) When declaring variables in a block scope, in a namespace scope, in for loop initialization statements, etc. The auto keyword can be used as a type specifier. Once the initializer type is determined, the compiler determines the type that will replace the auto keyword, using the rules to derive the template argument from the function call (see Details in the section Deriving the template argument # Other contexts). The auto keyword may be followed by modifiers such as const or &, which will participate in type inference. For example, if const auto& = expr; type i exactly matches the type of argument u in an imaginary template template<class U> void f(const U& u) if the function call f(expr) was compiled. Therefore, auto && can be displayed either as an lvalue reference or as an rvalue reference according to the initializer used in the range-based for loop. If auto is used to declare multiple variables, the inferred types must match. For example, auto = 0, d = 0.0; declaration auto = 0, d = 0.0; malformed while auto = 0, *p = &i; declaration auto = 0, *p = &i; correctly formed, and the car is displayed as int.
2) In a function declaration that uses the final syntax of the return type, the auto keyword does not automatically detect the type. This is only part of the syntax.
3) In a function declaration that does not use the final syntax of the return type, the auto keyword indicates that the type of the return value will be inferred from the operand of the return statement, using the rules to output the template argument.
4) If the declared type of the variable is decltype (auto), the auto keyword is replaced by the expression (or list of expressions) of its initializer, and the actual type is determined using the rules for decltype.
5) If the return type of the function is declared decltype (auto), the auto keyword is replaced with the operand of its return statement, and the actual type of the return value is determined using the rules for decltype.
6) The nested name specifier in the form auto :: is a placeholder that is replaced with a class or enumeration type in accordance with the rules for deriving a limited type placeholder.
7) Declaring a parameter in a lambda expression. (since C ++ 14) Declaring a function parameter. (TS concepts)
Notes Prior to C ++ 11, auto had storage duration specifier semantics. Mixing automatic variables and functions in one declaration, as in auto f() โ int, = 0; not allowed.
For more information: http://en.cppreference.com/w/cpp/language/auto