auto Keyword (Type Deduction)

The auto keyword deduces the type of a declared variable from its initialization expression.

auto declarator initializer;

Remarks

Starting with Visual C++ 2010, the auto keyword directs the compiler to deduce the type of a declared variable from its initialization expression. The previous meaning of the auto keyword as the specifier for the automatic storage-class is now an error.

To use the auto keyword, declare a variable with the auto keyword instead of a type, and specify an initialization expression. In addition, you can modify the auto keyword with specifiers and declarators such as const, volatile, pointer (*), reference (&), and rvalue reference (&&). The compiler evaluates the initialization expression and then uses that information to deduce the type of the variable.

The initialization expression can be an assignment (equal-sign syntax), direct initialization (function-style syntax), or operator new expression. Or the initialization expression can be the expression parameter in a for each, in statement. For more information, see Initializers.

Note that the auto keyword is a placeholder for a type, but is not a type itself. Therefore, the auto keyword cannot be used in casts or operators such as sizeof and typeid.

Use the /Zc:auto compiler option, which is the default in Visual C++ 2010, to tell the compiler to use the new meaning of the auto keyword. 

Usefulness

The auto keyword is a simple way to declare a variable that has a complicated type. For example, use the auto keyword to declare a variable where the initialization expression involves templates, pointers to functions, or pointers to members.

Use the auto keyword to declare and initialize a variable to a lambda expression. You cannot declare the type of the variable yourself because the type of a lambda expression is known only to the compiler. For more information, see Examples of Lambda Expressions.

Use the auto keyword, together with the decltype type specifier, to help write template libraries. Use auto and decltype to declare a template function whose return type depends on the types of its template arguments. Or, use auto and decltype to declare a template function that wraps a call to another function, and then returns whatever is the return type of that other function. For more information, see decltype Type Specifier.

Restrictions and Error Messages

The following table lists the restrictions on the use of the auto keyword, and the corresponding diagnostic error message that the compiler emits.

Error number

Description

C3530

The auto keyword cannot be combined with any other type-specifier.

C3531

A symbol that is declared with the auto keyword must have an initializer.

C3532

You incorrectly used the auto keyword to declare a type. For example, you declared a method return type or an array.

C3533, C3539

A parameter or template argument cannot be declared with the auto keyword.

C3534

A symbol that is declared with the auto keyword in a new expression must have an initializer. For more information, see operator new (<new>).

C3535

A method or template parameter cannot be declared with the auto keyword.

C3536

A symbol cannot be used before it is initialized. In practice, this means that a variable cannot be used to initialize itself.

C3537

You cannot cast to a type that is declared with the auto keyword.

C3538

All the symbols in a declarator list that is declared with the auto keyword must resolve to the same type. For more information, see Declarations.

C3540, C3541

The sizeof and typeid operators cannot be applied to a symbol that is declared with the auto keyword.

Brief Examples

This section contains code fragments that illustrate some of the ways the auto keyword can be used.

The following declarations are equivalent. In the first statement, variable j is declared to be type int. In the second statement, variable k is deduced to be type int because the initialization expression (0) is an integer.

   int j = 0;  // Variable j is explicitly type int.
   auto k = 0; // Variable k is implicitly type int because 0 is an integer.

The following declarations are equivalent, but the second declaration is simpler than the first. One of the most compelling reasons to use the auto keyword is simplicity.

   map<int,list<string>>::iterator i = m.begin(); 
   auto i = m.begin(); 

The following code fragment declares the type of variables it and elem when the for and for each loops start.

   #include <deque>
   int main()
   {
      deque<double> dq1(2,0.1);
      for (auto it = dq1.begin(); it != dq1.end(); ++it)
      { /* ... */ }
      for each(auto elem in dq1) 
      { /* ... */ }
   }

The following code fragment uses the new operator and pointer declaration to declare pointers.

   double x = 12.34;
   auto *y = new auto(x), **z = new auto(&x);

The following code fragment declares multiple symbols in each declaration statement (although this is poor coding style). Note that all the symbols in each statement resolve to the same type.

   auto x = 1, *y = &x, **z = &y; // Resolves to int.
   auto a(2.01), *b (&a);         // Resolves to double.
   auto c = 'a', *d(&c);          // Resolves to char.
   auto m = 1, &n = m;            // Resolves to int.

The following code fragment uses the conditional operator (?:) to declare variable x as an integer with a value of 200.

   int v1 = 100, v2 = 200;
   auto x = v1 > v2 ? v1 : v2;

The following code fragment initializes variable x to type int, variable y to a reference to type const int, and variable fp to a pointer to a function that returns type int.

   int f(int x) { return x; }
   int main()
   {
   auto x = f(0);
   const auto & y = f(1);
   int (*p)(int x);
   p = f;
   auto fp = p;
   ...
   }

See Also

Reference

auto Keyword

Storage-Class Specifiers

C++ Keywords

/Zc:auto (Deduce Variable Type)

sizeof Operator

typeid

operator new (<new>)

Declarations

Examples of Lambda Expressions

Initializers

decltype Type Specifier