C++11 auto keyword

In this post we will look at the C++11 auto keyword. What it is? and how to use the ‘auto keyword’ in our program will be discussed.

In any program if we need a value we would declare the type and a variable and assign a value to it or not. But to declare a variable we need to know the specific type of the value that we require. This is not a problem until we know the type of the data we need. However, sometimes it is not possible to determine exactly the specific type of the value the program may require. In such case, we can use the keyword auto.By using auto data we are forcing the compiler to figure out the type of the variable from the initializer.

Note:: auto keyword is a new feature added in C++11.You can read below on how to enable C++11 in Code::blocks

A simple code using ‘auto’ is shown below.

auto ii=90 ;

cout<< ii ;

for(auto i=0 ; i>10; i++ )
  cout<< i*i ;

In the program above we have not declare the type of ‘ii’ and ‘i’ variable but instead we gave the type as ‘auto’.And since the initializer of ‘ii’ and ‘i’ are integer literals the compiler deduce the type of ‘ii’ and ‘i’ as int type. Note here we did not write the type of ‘ii’ and ‘i’ as int type explicitly but the compiler deduced it from the value assigned to them -90 and 0 respectively-for us.

Like wise if the value assigned to ‘auto’ variable is a real number -number with fractional value- the compiler deduce it as double type, it is not deduced as float type. In case if we assign a variable whose type we already know to auto variable, then the type deduce is same as the type of the initializer variable .

int i=90 ;
auto ii=i ;   //ii is int since i is int type
auto m=87 ;   //m is int type since 87 is an integer

auto g=9.0998 ;   //g is double type 
auto mms=i+ii ;   //mms is int type since i and ii is int type

You can check the type of the auto variable by using using typeid() operator but do not forget to include <typeinfo> header’s name.

Declaring multiple variable with auto

Like the traditional data type which allows multiple variable declaration of a single type,we can also declare multiple variables for the auto type.But in this case, the initializer must not conflict with one another.By this we mean all the variables declare for one auto must have the initializer of the same type.Consider the code below

auto str=”string” , &st=str ;   /*Works fine str and st are const char* type */

auto str=”string” , ii=90 ;   //error! 
/* str initializer is string type but ‘ii’ initializer is int type; so type conflict here */

auto val=34 , ff=90.447; //error!
/* type conflict again,’val’ initializer is integer but ‘ff’ initializer is floating point value*/

In code::blocks for the second line(line 3) and third line(line 7) you will get an error message as “inconsistent deduction for ‘auto’: ‘const char*’ and then ‘int’ “.

Deducing the type when const variable is used as initializer

While initializing const variable to auto type variable we must note two things:

**i):The auto variable is deduce as constant only when the initializer are constant reference or constant pointer.This means whether the initializer is const or not,if it is not a pointer the auto variable will be deduced as non-constant type.Consider the code below.

int i=90 ;

const int ci=i , *ip=&ci ;

auto ai1=ci ;   
/* ai1 is int type not ‘const int’ type although ci is ‘const int’ type */

auto ai2=*ip ;   //ai2 is int type not ‘const int’ type

auto ai3=&ci , *ip1=ip ;   /* ai3 and ip1 are ‘const int*’ type */

In the fourth line(Code line 8) the initializer is an integer literal not a pointer, so ai2 is deduced as int type not ‘const int’ type or const int* type.

In the fifth line (code line 10),ai3 and ip1 is deduce as const int*.We know that the only type to which a reference can be assigned as an initializer is a pointer. And since the initializer of both the variable: ci and ip, is a const type the type deduce is ‘const int*’.

**ii): The auto reference and pointers will be interpreted as const only when they are assigned to a constant object. Here it means if the auto variable has a ‘&’ sign or ‘*’ sign in front, then they will be deduced as const reference or const pointer type only when the initializer is a const type.Consider the code below.

int i=90 ;
const int ci=i , *ip=&ci ;

auto &mi=ci ,   //mi is ‘const int&’ type
auto *p2=&ip ;   //p2 is ‘const int**’ type

auto &k=i , *g=&ci ;   //Error!!
/*Inconsistent type: k is ‘int&’ type but g is ‘const int*’ type */

auto &ref=i ; //ref is ‘int&’ type

The code above shows only how to make a reference or a pointer const.

Suppose if we want to make auto variable a constant type when initialized with constant variable then we can do so by mentioning the keyword const before the auto keyword explicitly.

const int ci=90 ;
const auto ii=ci ;   //ii is a const int type

Here the const qualifier is mentioned explicitly so the type of ‘ii’ deduced is ‘cont int’ type.

Enabling C++11 in Code::blocks

If you have not enable C++11 in Code::blocks then you may get an error while compiling your program which include C++11 features.You can follow the steps below to enable C++11 features in Code::blocks.

Step 1:
Go to Settings->Compiler and you will see a new Window whose name is “Global compiler settings” .The screen shot is given below.

C++11 auto keyword enable in Code:blocks

And check the box which says “Have g++ follow the C++11 ISO C++ language standard [-std=C++11] ” and click ok.

After this is done you will be able to use auto , decltype() and many more features of C++11.