命名空间
变体
操作

decltype 说明符 (C++11 起)

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

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

目录

[编辑] 语法

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

[编辑] 解释

1) 如果参数是未加括号的id-表达式或未加括号的类成员访问表达式,则 decltype 产生该表达式所命名的实体的类型。如果没有这样的实体,或者如果参数命名了一组重载函数,则程序格式不正确。

如果参数是命名结构化绑定的未加括号的id-表达式,则 decltype 产生引用类型(在结构化绑定声明的规范中描述)。

(C++17 起)

如果参数是命名非类型模板参数的未加括号的id-表达式,则 decltype 产生模板参数的类型(在模板参数声明了占位符类型的情况下,执行任何必要的类型推导之后)。即使实体是模板参数对象(一个 const 对象),该类型也是非 const 的。

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

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

(C++17 前)

如果表达式是纯右值(除了 (可能加括号的) 即时调用(C++20 起),则不会从该纯右值具体化临时对象:这样的纯右值没有结果对象。

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

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

decltype 在声明难以或不可能使用标准符号声明的类型时非常有用,例如与 lambda 相关的类型或依赖于模板参数的类型。

[编辑] 注意

功能测试宏 标准 特性
__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)
  • 待定 Decltype 说明符 [dcl.type.decltype]
  • C++14 标准 (ISO/IEC 14882:2014)
  • 待定 Decltype 说明符 [dcl.type.decltype]
  • C++11 标准 (ISO/IEC 14882:2011)
  • 待定 Decltype 说明符 [dcl.type.decltype]

[编辑] 参见

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