C++11学习笔记(2) —— decltype

1. 简介

当我们想要方便的定义一个变量时,可以通过使用 auto 来定义变量,由编译器根据上下文推导出变量类型。但是,当我们想要推导一个表达式的返回结果类型时,auto 就无能为力了。decltype 可以解决该问题。

2. 函数返回类型推导

使用decltype可以动态获取函数的返回类型,如下所示

[cpp]  view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. int fun1(int n1, int n2)  
  5. {  
  6.     return n1*n2;  
  7. }  
  8.   
  9. templateclass T, class U >  
  10. auto fun2(T t, U u) -> decltype(t*u)  
  11. {   
  12.     return t*u;  
  13. }  
  14.   
  15. int _tmain(int argc, _TCHAR* argv[])  
  16. {  
  17.     decltype( fun1(1,2) )  var1;  
  18.     decltype( fun2(3,2) )  var2;  
  19.     decltype( fun2(4.0,2) )  var3;  
  20.   
  21.     cout<< "Type of var1: " << typeid(var1).name() << endl;  
  22.     cout<< "Type of var2: " << typeid(var2).name() << endl;  
  23.     cout<< "Type of var3: " << typeid(var3).name() << endl;  
  24.   
  25.     return 0;  
  26. }  

运行结果如下图所示:decltype 根据函数的返回值,动态的获取其数据类型。


3. 在尾部声明函数返回类型

在之前版本的C++中,对函数的返回值的声明必须放在函数前面,C++11 的新特性允许我们将函数返回值类型的声明放在末尾,前提是需要auto的配合使用,例如上述代码中的

[cpp]  view plain copy
  1. templateclass T, class U >  
  2. auto fun2(T t, U u) -> decltype(t*u)  
  3. {   
  4.     return t*u;  
  5. }  

该模板函数的返回值类型首先使用 auto 声明,然后 使用 -> decltype 来推导出真正的返回值类型,编译器可以根据传入的不同参数类型进行动态的推导。注意,不可以使用如下的形式:

[cpp]  view plain copy
  1. templateclass T, class U >  
  2. decltype(t*u) fun(T t, U u)    // Error!  
  3. {   
  4.     return t*u;  
  5. }  

4. 对左值表达式的推导

在使用decltype 时需要注意,如果表达式结果是左值,那么推导结果为引用类型,如下例所示:

[cpp]  view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. int _tmain(int argc, _TCHAR* argv[])  
  5. {  
  6.     int i = 10;  
  7.     decltype( i ) var1;  
  8.   
  9.     //Compile Error: error C2530: 'var2' : references must be initialized  
  10.     //decltype( (i) ) var2;  
  11.   
  12.     decltype( (i) ) var3 = i;  
  13.     cout<< "Address of    i: " << &i << endl;  
  14.     cout<< "Address of var1: " << &var1 << endl;  
  15.     cout<< "Address of var3: " << &var3 << endl;  
  16.   
  17.     return 0;  
  18. }  

运行结果如下图所示:


i 为整型的变量, decltype(i) 是对变量 i 进行类型推导,其结果为 int 型。 ( i ) 加了括号以后是表达式, decltype( ( i ) ) 实际上是对表达式 ( i ) 进行类型的推导,由于该表达式的结果是个左值,因此其推导出的数据类型为 Int & , 有输出结果可以看出, var3  和  i  的地址相同, var3 是 i 的一个引用, 但是 var1 是 Int 类型,其数值是 i 的一个拷贝!

转载自http://blog.csdn.net/fire_lord/article/details/8499603


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值