C++11 decltype() keyword.
The C++11 decltype() keyword can deduce a type of the variable using the expression given under it’s bracket.Unlike the ‘auto’ keyword decltype() can deduce the type of the variable without requiring to assign any initializer to the variable. A simple implementation of decltype() is given below.
Link :C++11 auto keyword
int i ; decltype(i) ii ; // ii is int type
‘ii’ is int type.The decltype() deduced the type of ‘ii’ from the variable ‘i’ pass an argument.In the example, the expression written under the bracket is a variable but it can be also a reference or pointers or function name or an expression.
If the expression is a reference then the type deduce is a reference and of the 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 pointer as an expression the deduce type can vary depending on whether the ‘*’ sign is used or not. If the expression used is a pointer and has ‘*’ sign then the type deduce is a reference but if just the pointer name is used without the ‘*’ sign then the type deduce is a pointer. Look at the code 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!! because 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 the expression while using 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.
Using an evaluation expression as the decltype() epression
Suppose a variable or any of the expression discussed above is not used but rather an expression having an operator 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 need not perform any evaluation literally).
int i=0 , *ip=&i ; decltype( *ip + i ) ex ; //ex is an int type
Using object as an expression
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 applicable. Look at the code below.
class T { public: 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