1. 简介
当我们想要方便的定义一个变量时,可以通过使用 auto 来定义变量,由编译器根据上下文推导出变量类型。但是,当我们想要推导一个表达式的返回结果类型时,auto 就无能为力了。decltype 可以解决该问题。
2. 函数返回类型推导
使用decltype可以动态获取函数的返回类型,如下所示
- #include<iostream>
- using namespace std;
- int fun1(int n1, int n2)
- {
- return n1*n2;
- }
- template< class T, class U >
- auto fun2(T t, U u) -> decltype(t*u)
- {
- return t*u;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- decltype( fun1(1,2) ) var1;
- decltype( fun2(3,2) ) var2;
- decltype( fun2(4.0,2) ) var3;
- cout<< "Type of var1: " << typeid(var1).name() << endl;
- cout<< "Type of var2: " << typeid(var2).name() << endl;
- cout<< "Type of var3: " << typeid(var3).name() << endl;
- return 0;
- }
运行结果如下图所示:decltype 根据函数的返回值,动态的获取其数据类型。
3. 在尾部声明函数返回类型
在之前版本的C++中,对函数的返回值的声明必须放在函数前面,C++11 的新特性允许我们将函数返回值类型的声明放在末尾,前提是需要auto的配合使用,例如上述代码中的
- template< class T, class U >
- auto fun2(T t, U u) -> decltype(t*u)
- {
- return t*u;
- }
该模板函数的返回值类型首先使用 auto 声明,然后 使用 -> decltype 来推导出真正的返回值类型,编译器可以根据传入的不同参数类型进行动态的推导。注意,不可以使用如下的形式:
- template< class T, class U >
- decltype(t*u) fun(T t, U u) // Error!
- {
- return t*u;
- }
4. 对左值表达式的推导
在使用decltype 时需要注意,如果表达式结果是左值,那么推导结果为引用类型,如下例所示:
- #include<iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- int i = 10;
- decltype( i ) var1;
- //Compile Error: error C2530: 'var2' : references must be initialized
- //decltype( (i) ) var2;
- decltype( (i) ) var3 = i;
- cout<< "Address of i: " << &i << endl;
- cout<< "Address of var1: " << &var1 << endl;
- cout<< "Address of var3: " << &var3 << endl;
- return 0;
- }
运行结果如下图所示:
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