代码点读机 / C++ / decltype

decltype C++ · 关键字

类型推导说明符,根据表达式的类型生成对应的类型名;引用表达式会保留引用,这是它和 auto 的主要区别。

怎么用

decltype(表达式) 变量名;

小例子

int x = 5;
int& rx = x;
decltype(rx) y = x;
y = 10;
std::cout << x;

运行结果

10

提醒:decltype((x)) 通常是引用类型,而 decltype(x) 是值类型

官方文档:https://en.cppreference.com/w/cpp/language/decltype

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← C++全部词条