decltype
     
     specifier
     
      (since C++11)
     
    
    检查实体的声明类型或表达式的类型及值类别。
| 目录 | 
语法
| 
          
           decltype (
          
         实体
          
           )
          
          | (1) | ||||||||
| 
          
           decltype (
          
         表达式
          
           )
          
          | (2) | ||||||||
说明
| 若实参为无括号的 标识表达式 且命名一个 结构化绑定 ,则 decltype 产生 被引用类型 (在结构化绑定声明的规范中描述)。 | (C++17 起) | 
| 若实参为无括号的 标识表达式 且命名一个 常量模板形参 ,则 decltype 产生该模板形参的类型(若模板形参以占位符类型声明,则在执行必要类型推导后)。即使该实体是模板形参对象(为 const 对象),该类型亦为非 const。 | (C++20 起) | 
        T
       
       的任何其他表达式,且
      | 若 表达式 是返回类类型纯右值的函数调用,或是 逗号表达式 且其右操作数为此类函数调用,则不会为该纯右值引入临时对象。 | (C++17 前) | 
| 若 表达式 是纯右值 (除了(可能带括号的) 立即调用 之外) (C++20 起) ,则不会从该纯右值 实质化 临时对象:此类纯右值没有结果对象。 | (C++17 起) | 
请注意,如果对象名称被括号括起,它将被视为普通的左值表达式,因此 decltype ( x ) 和 decltype ( ( x ) ) 通常是不同的类型。
       
        decltype
       
       在声明那些使用标准表示法难以或不可能声明的类型时非常有用,例如与 lambda 相关的类型或依赖于模板参数的类型。
      
注释
| 功能测试宏 | 值 | 标准 | 功能 | 
|---|---|---|---|
| 
           __cpp_decltype
           | 
           200707L
           | (C++11) | decltype | 
关键词
示例
#include <cassert> #include <iostream> #include <type_traits> struct A { double x; }; const A* a; decltype(a->x) y; // y 的类型是 double(声明类型) decltype((a->x)) z = y; // z 的类型是 const double&(左值表达式) template<typename T, typename U> auto add(T t, U u) -> decltype(t + u) // 返回类型依赖于模板参数 // 自 C++14 起可推导返回类型 { return t + u; } const int& getRef(const int* p) { return *p; } static_assert(std::is_same_v<decltype(getRef), const int&(const int*)>); auto getRefFwdBad(const int* p) { return getRef(p); } static_assert(std::is_same_v<decltype(getRefFwdBad), int(const int*)>, "仅返回 auto 无法实现完美转发。"); decltype(auto) getRefFwdGood(const int* p) { return getRef(p); } static_assert(std::is_same_v<decltype(getRefFwdGood), const int&(const int*)>, "返回 decltype(auto) 可完美转发返回类型。"); // 替代方案: auto getRefFwdGood1(const int* p) -> decltype(getRef(p)) { return getRef(p); } static_assert(std::is_same_v<decltype(getRefFwdGood1), const int&(const int*)>, "返回 decltype(返回表达式) 同样能完美转发返回类型。"); int main() { int i = 33; decltype(i) j = i * 2; static_assert(std::is_same_v<decltype(i), decltype(j)>); assert(i == 33 && 66 == j); auto f = [i](int av, int bv) -> int { return av * bv + i; }; auto h = [i](int av, int bv) -> int { return av * bv + i; }; static_assert(!std::is_same_v<decltype(f), decltype(h)>, "lambda 函数的类型是唯一且无名的"); decltype(f) g = f; std::cout << f(3, 3) << ' ' << g(3, 3) << '\n'; }
输出:
42 42
参考文献
| 扩展内容 | 
|---|
| 
 
 
 
 
 
 
 
 
 
 | 
| 本节内容不完整 原因:需要修正。参见: 讨论:错误引用 。 | 
参见
| 
           auto
          说明符
         
         
          (C++11) | 指定从表达式推导出的类型 | 
| 
           
            
             
              (C++11)
             
            
           
           | 在未求值上下文中获取模板类型参数的引用 (函数模板) | 
| 
           
            
             
              (C++11)
             
            
           
           | 检查两个类型是否相同 (类模板) | 
| 
          
           
            C 文档
           
          
          关于
          
           
            typeof
           
          
          | |