C++11 using decltype() keyword.


The decltype() keyword can deduce a type for the variable using the expression given under it’s bracket.Unlike the auto type decltype() can deduce the type of the variable without requiring to assign the expression to the variable.A simple implementation of decltype() is given below.
 

int i ;
decltype(i) ii ; // ii is int type

 
‘ii’ is int type since i is int type.In this case the expression written under the bracket is a variable but it can be also a reference or pointers or function name or an expression itself.

If the expression is a reference then the type deduce is a reference of that data type.
 

int i , &ri=i ;
int i1=0 ;
decltype(ri) xi=i1 ; //xi is a reference to i1
decltype( ri ) rri ; //error : rri is a reference and needs initialization
decltype( ri ) rri=90 ; //error : rri is a reference

 
While using a pointers as an expression the deduce type can vary depending on whether the ‘*’ sign is used or not.If the expression used is pointer and has ‘*’ sign then the type deduce is a reference but if just a pointer name is used without the ‘*’ sign then the type deduce is a pointer to the data type of the expression.Look at the codes below.
 

int i , *pi=&i ;
int x=0 ;
decltype( *pi ) xp=x ; //xp is a reference to x or xp is ‘int &’ type.
decltype( *pi ) z=&i ; //error cause z is a reference type
decltype( pi ) m=&i ; /* works fine cause m is a pointer to int type or m is ‘int *’ type */

 
We can also use a function name as an expression for the decltype().In this case the type deduce is the type returned by the function but note that the function is not evaluated( not called).
 

char func() { }

decltype( func() ) c ; //c is a char type
decltytpe( func() ) cc=90 ;

cout << cc ;

I am sure the output is a character value of 99.
 
If suppose a variable or any of the expression discussed above is not used but an expression is used then the type deduce is the type of the resultant value that will be obtained when the whole expression is evaluated(the expression is not actually evaluated).

int i=0 , *ip=&i ;
decltype( *ip + i ) ex ; ///ex is an int type




If an object of a class is used instead of the built-in data type as an expression for the decltype() the concept mentioned above is also applicable in this case.Look at the code below.

class T
{
publc:
  int i ;
  T( int x) { i=x; }
  string func() { }
}

int main()
{
T t(9) , &rt=t , *pt=&t ;
decltype( t ) tt(0) ; //tt is T type object
cout << tt.i << endl ;

decltype( rt ) g=t ; //g is a reference to T type
decltype( *pt ) s ; //error cause s is a reference to T type

decltype( t.func() ) st=”Happy chakra” ; ///st is string type
cout<< st ;

cin.get() ;
return 0 ;
}

 


*Side Note

If a variable under a bracket is used as an expression for the decltype() then the type deduce is a reference type of that variable not the type itself.

int i ;
decltype( (i) ) x=i; // x is a reference to i
decltype( (i) ) y ; // error cause y needs initialization

 


Related Link

->C++11 using auto keyword.
 
->C++11 Smart pointer : Shared pointer ; why is it call shared pointers?