In a program if we need a certain value we would declare it’s type and a variable and assigned a value to it or not.But to declare a variable we need to know the specific data type of the value that we require.This is not a problem until and unless we know the specific 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 as a general data type and let the compiler 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 a type for ‘ii’ and ‘i’ variable but instead we gave the type as ‘auto’.And since the initializer of ‘ii’ and ‘i’ is an 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 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 variable assigned.
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 for a single type,we can also declare multiple variables for auto type.But in this case note,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” , 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 and third line 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 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.
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 the initializer is an integer literal not a pointer,so ai2 is deduce as int type not ‘const int’ type or const int* type.
In the fifth line,ai3 and ip1 is deduce as const int*.We know that the only type which can be assigned a reference-initializer is a pointer type.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 constant object.Here it means if the auto variable has a ‘&’ sign or ‘*’ sign in front,then they will be deduce as const reference or const pointer type only when the intializer is a const type.Consider the code below.
auto *p2=&ip ; ///p2 is ‘const int*’ type
auto &k=i , *g=&ci ; /*Error inconsistent type: k is ‘int&’ type but g ‘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 auto ii=ci ; ///ii is a const int type
Here the const qualifier is mentioned explicitly so the type of ‘ii’ deduce 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.
Go to Settings->Compiler and you will see a new Window whose name is “Global compiler settings” .The screen shot is given below.
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.