首页 > 解决方案 > decltype((x)) 带双括号是什么意思?

问题描述

非常简单的问题,我无法用谷歌搜索答案。

例如:

int a = 0;
int& b = x;
int&& c = 1;

decltype((a)) x; // what is the type of x?
decltype((b)) y; // what is the type of y?
decltype((c)) z; // what is the type of z?

也许我应该将 x、y 和 z 分配给某个值以获得不同的结果,我不确定。

编辑: 根据以下双括号将示例int转换为参考的站点: https ://github.com/AnthonyCalandra/modern-cpp-features#decltype

int a = 1; // `a` is declared as type `int`
int&& f = 1; // `f` is declared as type `int&&`
decltype(f) g = 1; // `decltype(f) is `int&&`
decltype((a)) h = g; // `decltype((a))` is int&

标签: c++c++11referencervalue-referencedecltype

解决方案


它们都是 type int&

添加括号 like(a)使它们成为表达式(而不是entity),它们都是左值(作为命名变量);然后decltype屈服于T&,即int&这里。

...

4) 如果参数是任何其他类型的表达式T,并且

...

b) 如果表达式的值类别是左值,则 decltype 产生 T&

...

您可以使用此LIVE DEMO(来自编译错误消息)检查实际类型。


推荐阅读