The auto type specifier (C++0x)

Note: C++0x is a new version of the C++ programming language standard. This is a draft standard and has not been officially adopted in its entirety. The implementation of C++0x is based on IBM's interpretation of the draft C++0x standard and is subject to change at any time without notice. IBM makes no attempt to maintain compatibility with earlier releases and therefore the C++0x language extension should not be relied on as a stable programming interface.

C++0x introduces the keyword auto as a new type specifier. auto acts as a placeholder for a type to be deduced from the initializer expression of a variable. With auto type deduction enabled, you no longer need to specify a type while declaring a variable. Instead, the compiler deduces the type of an auto variable from the type of its initializer expression.

The following examples demonstrate the usage of auto type deduction.
auto x = 1;     //x : int

float* p;
auto x = p;     //x : float*
auto* y = p;    //y : float*

double f();
auto x = f();             //x : double
const auto& y = foo();    //y : const double&

class R;
R* h();
auto* x = h();    //x : R*
auto y = h();     //y : R*

int& g();
auto x = g();           //x : int
const auto& y = g();    //y : const int&
auto* z = g();          //error, g() does not return a pointer type 

By delegating the task of type deduction to the compiler, auto type deduction increases programming convenience, and potentially eliminates typing errors made by programmers. Auto type deduction also reduces the size and improves the readability of programs.

The following two examples demonstrate the benefits of enabling auto type deduction. The first example does not enable auto type deduction.
for (vector<int>::iterator i = vec.begin(); i < vec.end(); i++)  
{
   int* a = new int(1);
   //...
}
With auto type deduction enabled, the first example can be simplified as follows:
for (auto i = vec.begin(); i < vec.end(); i++)  
{
   auto a = new auto(1);
   //...
}
The following rules and constraints apply to the use of auto as a type specifier in auto type deduction.
  • Auto type deduction cannot deduce array types.
    int x[5];
    auto y[5] = x;    //error, x decays to a pointer, 
                      //which does not match the array type
  • Auto type deduction cannot deduce cv-qualifier or reference type from the initializer.
    int f();
    auto& x = f();    //error, cannot bind a non-const reference
                      //to a temporary variable
    int& g();
    auto y = g();     //y is of type int
    auto& z = g();    //z is of type int&
  • Auto type deduction supports multi-variable auto declarations. If the list of declarators contains more than one declarator, the type of each declarator can be deduced independently. If the deduced type is not the same in each deduction, the program is ill-formed.
    auto x=3, y=1.2, *z=new auto(1);     //error y: deduced as double, 
                                         //but was previously deduced as int
  • The name of the object that is declared can not be used in its initializer expression.
    auto x = x++;     //error
  • auto can not be used in function parameters.
    int func(auto x = 3)     //error
    {
        //...
    }    
Note: In C++0x, the keyword auto is no longer used as a storage class specifier.