cpp/language/auto

For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer.

Syntax
@1@ type is deduced using the rules for. @2@ type is, where is the initializer or ones used in return statements.

The placeholder may be accompanied by modifiers, such as  or, which will participate in the type deduction.

Explanation
A placeholder type specifier may appear in the following contexts:

  in the type specifier sequence of a variable: as a type specifier. The type is deduced from the initializer. If the placeholder type specifier is, the variable type is deduced from the initializer using the rules for  from a function call (see  for details). For example, given, the type of is exactly the type of the argument  in an imaginary template  if the function call  was compiled. Therefore, may be deduced either as an lvalue reference or rvalue reference according to the initializer, which is used in range-based for loop.

If the placeholder type specifier is used to declare multiple variables, the deduced types must match. For example, the declaration is ill-formed, while the declaration  is well-formed and the  is deduced as.

 in the type-id of a. The type is deduced from the initializer. For (where  contains a placeholder type, init is either a parenthesized initializer or a brace-enclosed initializer list), the type of  is deduced as if for variable  in the invented declaration.

 in the return type of a  or lambda expression:. The return type is deduced from the operand of its. See.

 in the parameter declaration of a :. Its type is deduced from the corresponding argument.



{{rrev|since=c++20|1=

Type constraint
If is present, let  be the type deduced for the placeholder, the  introduces a  as follows:


 * If is, then the constraint expression is ;
 * otherwise ( is without an argument list), the constraint expression is.

Deduction fails if the constraint expression is invalid or returns. }}