[c++11]decltype

参考:

decltype (C++)

decltype specifier

decltype类型说明符是c++11新增的特性,能够生成指定表达式的类型。语法如下:

decltype( expression )

推导规则

编译器使用以下规则推导参数expression的类型

  1. 如果参数expression是一个标识符(identifier)或者类成员访问(class member access),那么decltype(expression)是该实体(entity)的类型
  2. 如果参数expression是一个函数或者重载操作符的调用,那么decltype(expression)返回函数值类型。忽略重载运算符周围的括号
  3. 如果参数expression是一个rvalue,那么decltype(expression)expression的类型;如果是一个lvalue,那么结果是对expression类型的lvalue引用

示例

int var;
const int&& fx();
struct A { double x; }
const A* a = new A();

推导结果如下: