decltype((x)) 用双括号是什么意思

decltype((x)) with double brackets what does it mean?

本文关键字:是什么 意思 decltype      更新时间:2023-10-16

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

例如:

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&

它们都属于 int& 类型。

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

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

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

您可以使用此实时演示(从编译错误消息中(检查实际类型。

根据C++ Primer

当我们decltype应用于没有任何括号的变量时,我们得到 该变量的类型。如果我们把变量的名字包装在一个或 更多组括号,编译器会将操作数计算为 表达。变量是可以是左侧的表达式 的作业。因此,对此类表达式的decltype会产生 参考资料:

// decltype of a parenthesized variable is always a reference
decltype((i)) d; // error: d is int& and must be initialized
decltype(i) e;   // ok: e is an (uninitialized) int

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

https://en.cppreference.com/w/cpp/language/decltype

据我了解,(x)是一个空表达式,它返回对x的引用。因此

  • decltype(x) int
  • declytpe((x)) int&