命名空间
变体
操作

decltype 说明符 (C++11 起)

来自 cppreference.cn
< cpp‎ | 语言
 
 
C++ 语言
 
 

检查实体的声明类型或表达式的类型和值类别。

目录

[编辑] 语法

decltype ( 实体 ) (1)
decltype ( 表达式 ) (2)

[编辑] 解释

1) 如果实参是无圆括号的 标识表达式 或无圆括号的 类成员访问 表达式,则 decltype 产生由此表达式命名的实体的类型。如果不存在此类实体,或者如果实参命名了一组重载函数,则程序是非良构的。

如果实参是命名结构化绑定的无圆括号的 标识表达式,则 decltype 产生引用类型(在结构化绑定声明的规范中描述)。

(C++17 起)

如果实参是命名非类型模板形参的无圆括号的 标识表达式,则 decltype 产生模板形参的类型(如果在使用占位符类型声明模板形参后执行任何必要的类型推导)。即使实体是模板形参对象(它是 const 对象),该类型也是非 const 的。

(C++20 起)
2) 如果实参是任何其他类型为 T 的表达式,并且
a) 如果 表达式值类别xvalue,则 decltype 产生 T&&
b) 如果 表达式 的值类别是 lvalue,则 decltype 产生 T&
c) 如果 表达式 的值类别是 prvalue,则 decltype 产生 T

如果 表达式 是返回类类型的 prvalue 的函数调用,或者是右操作数是此类函数调用的 逗号表达式,则不会为该 prvalue 引入临时对象。

(直到 C++17)

如果 表达式 是 prvalue,但不是(可能带圆括号的)立即调用(C++20 起),则不会从该 prvalue 物化临时对象:此类 prvalue 没有结果对象。

(C++17 起)
由于未创建临时对象,因此类型不必是完整的,也不必具有可用的析构函数,并且可以是抽象的。此规则不适用于子表达式:在 decltype(f(g())) 中,g() 必须具有完整类型,但 f() 不需要。

请注意,如果对象的名称用圆括号括起来,则将其视为普通的左值表达式,因此 decltype(x)decltype((x)) 通常是不同的类型。

当声明难以或不可能使用标准表示法声明的类型(例如 lambda 相关类型或依赖于模板形参的类型)时,decltype 非常有用。

[编辑] 注解

特性测试宏 Std 特性
__cpp_decltype 200707L (C++11) decltype

[编辑] 关键字

decltype

[编辑] 示例

#include <cassert>
#include <iostream>
#include <type_traits>
 
struct A { double x; };
const A* a;
 
decltype(a->x) y;       // type of y is double (declared type)
decltype((a->x)) z = y; // type of z is const double& (lvalue expression)
 
template<typename T, typename U>
auto add(T t, U u) -> decltype(t + u) // return type depends on template parameters
                                      // return type can be deduced since 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*)>,
    "Just returning auto isn't perfect forwarding.");
decltype(auto) getRefFwdGood(const int* p) { return getRef(p); }
static_assert(std::is_same_v<decltype(getRefFwdGood), const int&(const int*)>,
    "Returning decltype(auto) perfectly forwards the return type.");
 
// Alternatively:
auto getRefFwdGood1(const int* p) -> decltype(getRef(p)) { return getRef(p); }
static_assert(std::is_same_v<decltype(getRefFwdGood1), const int&(const int*)>,
    "Returning decltype(return expression) also perfectly forwards the return type.");
 
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)>,
        "The type of a lambda function is unique and unnamed");
 
    decltype(f) g = f;
    std::cout << f(3, 3) << ' ' << g(3, 3) << '\n';
}

输出

42 42

[编辑] 参考

扩展内容
  • C++23 标准 (ISO/IEC 14882:2024)
  • 9.2.9.5 Decltype 说明符 [dcl.type.decltype]
  • C++20 标准 (ISO/IEC 14882:2020)
  • 9.2.8.4 Decltype 说明符 [dcl.type.decltype]
  • C++17 标准 (ISO/IEC 14882:2017)
  • TBD Decltype 说明符 [dcl.type.decltype]
  • C++14 标准 (ISO/IEC 14882:2014)
  • TBD Decltype 说明符 [dcl.type.decltype]
  • C++11 标准 (ISO/IEC 14882:2011)
  • TBD Decltype 说明符 [dcl.type.decltype]

[编辑] 参见

auto 说明符 (C++11) 指定从表达式推导的类型 [编辑]
(C++11)
获取模板类型实参对象的引用,用于未求值语境
(函数模板) [编辑]
(C++11)
检查两个类型是否相同
(类模板) [编辑]
C 文档 关于 typeof