首页 > 代码库 > 第二十三章 C++11特性之decltype

第二十三章 C++11特性之decltype

decltype

类似于sizeof操作符,decltype也不需对其操作数求值。粗略来说,decltype(e)返回类型前,进行了如下推导:

1.若表达式e指向一个局部变量、命名空间作用域变量、静态成员变量或函数参数,那么返回类型即为该变量(或参数)的“声明类型”;

2.若e是一个左值(lvalue,即“可寻址值”),则decltype(e)将返回T&,其中T为e的类型;

3.若e是一个x值(xvalue),则返回值为T&&;

4.若e是一个纯右值(prvalue),则返回值为T。

这些语义是为满足通用库编写者的需求而设计,但由于decltype的返回类型总与对象(或函数)的定义类型相匹配,这对编程新手来说也更为直观。更正式地说,规则1适用于不带括号的标识符表达式(id-expression)与类成员访问表达式。示例如下:

const int&& foo();const int bar();int i;struct A { double x; };

const A* a = new A();

decltype(foo()) x1; // 类型为const int&&

decltype(bar()) x2; // 类型为int

decltype(i) x3; // 类型为int

decltype(a->x) x4; // 类型为double

decltype((a->x)) x5; // 类型为const double&

由上可见,最后两个对decltype的调用,返回结果有所不同。这是因为,带括号的表达式(a->x)既非“标识符表达式”,亦非类访问表达式,因而未指向一个命名对象,而是一个左值,于是推导类型便为“指向表达式类型的引用”,亦即const double&。

decltype实际上有点像auto的反函数,auto可以让你声明一个变量,而decltype则可以从一个变量或表达式中得到类型,有实例如下:

 

int x = 3;decltype(x) y = x;

有人会问,decltype的实用之处在哪里呢,我们接着上边的例子继续说下去,如果上文中的加工产品的例子中我们想把产品作为返回值该怎么办呢?我们可以这样写:

template <typename Creator>auto processProduct(const Creator& creator) -> decltype(creator.makeObject()) {    auto val = creator.makeObject();    // do somthing with val}

 

返回值 decltype(表达式)

[返回值的类型是表达式参数的类型]

 

这个可也用来决定表达式的类型,就像Bjarne暗示的一样,如果我们需要去初始化某种类型的变量,auto是最简单的选择,但是如果我们所需的类型不是一个变量,例如返回值这时我们可也试一下decltype。

 

现在我们回看一些例子我们先前做过的,

 

[cpp] view plaincopy
 
  1. template <class U, class V>  
  2. void Somefunction(U u, V v)  
  3. {  
  4.     result = u*v;//now what type would be the result???  
  5.     decltype(u*v) result = u*v;//Hmm .... we got what we want  
  6. }  

 

 

在下面的一个段落我将会让你熟悉这个观念用 auto 和 decltype 来声明模板函数的返回值,其类型依靠模板参数。

 

 

1. 如果这个表达式是个函数,decltype 给出的类型为函数返回值的类型。

 

[cpp] view plaincopy
 
  1. int add(int i, int j){ return i+j; }  
  2. decltype(add(5,6)) var = 5;//Here the type of var is return of add() -> which is int  


2.如果表达式是一个左值类型,那么 decltype 给出的类型为表达式左值引用类型。

 

 

[cpp] view plaincopy
 
  1. struct M { double x; };  
  2.   
  3. double pi = 3.14;  
  4. const M* m = new M();  
  5. decltype( (m->x) ) piRef = pi;  
  6.   
  7.     // Note: Due to the inner bracets the inner statement is evaluated as expression,  
  8.     // rather than member ‘x‘ and as type of x is double and as this is lvale  
  9.     // the return of declspec is double& and as ‘m‘ is a const pointer   
  10.     // the return is actually const double&.  
  11.     // So the type of piRef is const double&  

 

3.非常重要的标记一下,decltype 不会执行表达式而auto会,他仅仅推论一下表达式的类型。

 

[cpp] view plaincopy
 
  1. int foo(){}  
  2. decltype( foo() ) x; // x is an int and note that   
  3.                      // foo() is not actually called at runtime  

 


跟踪返回类型:

这对 C++ 开发者来说是一个全新的特性,直到现在函数的返回类型必须放在函数名的前面。到了 C++11,我们也可以将函数返回值的类型放在函数声明后,当然仅需要用 auto 替代返回类型。现在我们想知道怎么做,让我们来寻找答案:

 

[cpp] view plaincopy
 
  1. template<class U, class V>  
  2. ??? Multiply(U u, V v)    // how to specifiy the type of the return value  
  3. {   
  4.    return u*v;  
  5. }  


我们明显的不能像这样:

 

 

[cpp] view plaincopy
 
  1. template<class U, class V>  
  2. decltype(u*v) Multiply(U u, V v)    // Because u & v are not defined before Multiply.  
  3.                      //  What to do...what to do !!!  
  4. {   
  5.    return u*v;  
  6. }  

 


这种情况我们可也使用 auto 然后当我们使用 decltype(u*v) 作为返回值这个类型便知晓了.

这是不是很酷?

 

[cpp] view plaincopy
 
    1. template<class U, class V>  
    2. auto Multiply(U u, V v) -> decltype(u*v)    // Note -> after the function bracet.  
    3. {   
    4.    return u*v;  
    5. }  

第二十三章 C++11特性之decltype