The auto type specifier (C++11)

Note: IBM supports selected features of C++11, known as C++0x before its ratification. IBM will continue to develop and implement the features of this standard. The implementation of the language level is based on IBM's interpretation of the standard. Until IBM's implementation of all the C++11 features is complete, including the support of a new C++11 standard library, the implementation may change from release to release. IBM makes no attempt to maintain compatibility, in source, binary, or listings and other compiler interfaces, with earlier releases of IBM's implementation of the new C++11 features.

C++11 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 = f();      //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.
vector<int> vec;
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:
vector<int> vec;
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++11, the keyword auto is no longer used as a storage class specifier.