Namespaces
Variants

decltype specifier (since C++11)

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const / volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous

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

目录

语法

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

说明

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

若实参为无括号的 标识表达式 且命名一个 结构化绑定 ,则 decltype 产生 被引用类型 (在结构化绑定声明的规范中描述)。

(C++17 起)

若实参为无括号的 标识表达式 且命名一个 常量模板形参 ,则 decltype 产生该模板形参的类型(若模板形参以占位符类型声明,则在执行必要类型推导后)。即使该实体是模板形参对象(为 const 对象),该类型亦为非 const。

(C++20 起)
2) 如果实参是类型 T 的任何其他表达式,且
a) expression 值类别 xvalue ,则 decltype 产生 T &&
b) expression 的值类别为 lvalue ,则 decltype 产生 T &
c) 表达式 的值类别为 纯右值 ,则 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;       // 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

参考文献

扩展内容
  • 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