带双括号的decltype((x))是什么意思?

2 c++ reference decltype rvalue-reference c++11

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

例如:

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?
Run Code Online (Sandbox Code Playgroud)

我不确定我应该为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&
Run Code Online (Sandbox Code Playgroud)

son*_*yao 6

它们都是类型int&

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

...

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

...

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

...

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


Ayx*_*xan 5

根据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
Run Code Online (Sandbox Code Playgroud)