C++11 decltype type deduction

Hits: 0

decltype type deduction

decltype

decltype (expression)   //Get the type of the expression--calculated at compile time

decltype is very similar in technology and usage to sizeof, both require the compiler to calculate the type at compile time, but sizeof returns an integer, and decltype gets the type.

Similarities and differences between decltype and auto:

  • Different from auto:

    The type deduction of decltypr is not like auto to obtain the type of the variable from the initialization expression of the variable declaration, decltype always takes an ordinary expression as a parameter and returns the type of the expression.

  • Same as auto:

    As a type indicator, decltype can use the obtained type to define another variable;
    declttype type deduction is also performed at compile time

# include  <iostream>
 # include  <typeinfo>
 using  namespace  std ;
 int  main ()
 {
     int a;
     decltype (a) b = 0 ;
     cout << "The type of a is" << typeid (a).name()< < endl ;
     cout << "The type of b is" << typeid (b).name()<< endl ;

    float c;

    double d;
     decltype (c + d ) e;
     cout << "The type of e is" << typeid (e).name()<< endl ;

    return 0;
}

output:

The type of a is i   //i represents int
type of b is i  
The type of e is d    //ddouble

It can be seen that the type of the variable b is declared by decltype(a), which means that b is the same type returned by the expression a; and the type of e is the same type returned by
the expression (c + d), and the type of c + d The type of the expression is double, and the type of all e is deduced as double by decltype.

Application of decltype

decltype deduction rules
Expressions are ordinary variables or ordinary expressions or class expressions. In this case, the type deduced using decltype is the same as the type of the expression.

#include <iostream>
#include <string>
using namespace std;

class Test
{
public:
    string text;
    static const int value = 110;
};

int main()
{
    int x = 99;
    const int &y = x;
    decltype(x) a = x;
    decltype(y) b = x;
    decltype(Test::value) c = 0;

    Test t;
    decltype(t.text) d = "hello, world";

    return 0;
}

variable a is deduced as int type variable b is deduced as const int & variable c is deduced as const int variable d
is deduced as string type

The expression is a function call, and the type deduced using decltype is the same as the return value of the function.

class  Test { ...};
 // function declaration 
int  func_int () ;                  // return value int 
int & func_int_r () ;               // return value int& 
int && func_int_rr () ;             // return value int&&

const  int  func_cint () ;           // return value is const int 
const  int & func_cint_r () ;        // return value is const int& 
const  int && func_cint_rr () ;      // return value is const int&&

const Test func_ctest () ;         // return value is const Test

int n = 100;
decltype(func_int()) a = 0;        
decltype(func_int_r()) b = n;    
decltype(func_int_rr()) c = 0;    
decltype(func_cint())  d = 0;    
decltype(func_cint_r())  e = n;    
decltype(func_cint_rr()) f = 0;    
decltype(func_ctest()) g = Test();

Variable a is deduced to be of type int
Variable b is deduced to be of type int&
Variable c is deduced to be of type int&&
Variable d is deduced to be of type int
Variable e is deduced to be of type const
int & The function func_cint () of
type const Test
returns a prvalue (data that no longer exists after the execution of the expression, that is, temporary data). For prvalues, only class types can carry const, volatile qualifiers, otherwise these two qualifiers need to be ignored, so the deduced variable d is of type int instead of const int.

The expression is an lvalue, or is surrounded by parentheses ( ), and decltype deduces a reference to the type of the expression (if there are const and volatile qualifiers, it cannot be ignored).

#include <iostream>
#include <vector>
using namespace std;

class Test
{
public:
    int num;
};

int  main ()  {
     const Test obj;
     //expression with parentheses 
    decltype (obj.num) a = 0 ;
     decltype ((obj.num)) b = a;
     //additional expression 
    int n = 0 , m = 0 ;
     decltype (n + m) c = 0 ;
     decltype (n = n + m) d = n;
     return  0 ;
}

obj.num is a member access expression of the class, conforming to scenario 1, so a is of type int obj.num with parentheses conforms to scenario 3, so b is of type
const int&. n+m gets an rvalue, conforming to scenario 1, so c is of type int n=n+m gets an lvalue n, conforming to scenario 3, so d
is of type int&

You may also like...

Leave a Reply

Your email address will not be published.