Blog Archives

Auto Storage Class Specifier (C++11)

The auto key word lets you explicitly declare a variable with automatic type. It specifies that the type of the variable that is being declared will be automatically deduced from its initializer. For functions, specifies that the return type is a trailing return type. You can declare auto variables in block scope, in namespace scope, in init statements of for loops, etc, the type of the variable may be omitted and the keyword auto may be used instead.


 auto variable initializer

auto function -> return type      

You can only apply the auto key word to names of variables declared in a block or to names of function parameters. By default they have automatic type so useless in a data declaration.


Any auto variable can be initialized only exception are parameters. If you do not explicitly initialize an automatic object, its value is indeterminate. If you provide an initial value, the expression representing the initial value can be any valid C or C++ expression. The object is then set to that initial value each time the program block that contains the object’s definition is invoked.

Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword auto as if using the rules for template argument deduction from a function call. The keyword auto may be accompanied by modifies, such as const or &, which will participate in the type deduction. For example, given const auto& i = expr;, the type of i is exactly the type of the argument u in an imaginary template template<class U> void f(const U& u) if the function call f(expr) was compiled. During a function declaration, the keyword auto does not perform automatic type detection. It only serves as a part of the trailing return type syntax.

 Life time

Variables with the auto storage class specifier has a local scope by default. A variable x that has automatic storage has a local scope i.e. block scope, in namespace scope, in init statements of for loops.  Each time a code block is entered, storage for auto objects defined in that block is made available. When the block is exited, the objects/variables are no longer available for use. static key word needs to be applied to have a static scope for auto variable.  If an auto object is defined within a function that is recursively invoked, memory is allocated for the object at each invocation of the block. Unless it is declared as static.


#include <iostream>

#include <cmath>

#include <typeinfo>

template<class T, class U>

auto add(T t, U u) -> decltype(t + u) // the return type of add is the type of operator+(T,U)


return t + u;


auto get_fun(int arg)->double(*)(double) // same as double (*get_fun(int))(double)


switch (arg) {

case 1: return std::fabs;

case 2: return std::sin;

default: return std::cos;



int main()


auto a = 1 + 2;

std::cout << “type of a: ” << typeid(a).name() << ‘\n’;

auto b = add(1, 1.2);

std::cout << “type of b: ” << typeid(b).name() << ‘\n’;

//auto int c; //compile-time error

auto d = {1, 2};

std::cout << “type of d: ” << typeid(d).name() << ‘\n’;



type of a: int
type of b: double
type of d: std::initializer_list<int>