带有双括号的 decltype((x)) 是什么意思?
decltype((x)) with double brackets what does it mean?
非常简单的问题,我无法google 得出答案。
例如:
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)
添加括号使它们成为 expressions(而不是 entity),它们都是左值(作为命名变量);然后 decltype
屈服于 T&
,即这里的 int&
。
...
4) If the argument is any other expression of type T
, and
...
b) if the value category of expression is lvalue, then decltype yields
T&
;
...
您可以使用此 LIVE DEMO(来自编译错误消息)检查实际类型。
Note that if the name of an object is parenthesized, it is treated as
an ordinary lvalue expression, thus decltype(x) and decltype((x)) are
often different types.
https://en.cppreference.com/w/cpp/language/decltype
据我了解,(x)
是一个空表达式,returns 是对 x
的引用。于是
decltype(x)
是 int
declytpe((x))
是 int&
根据C++ Primer:
When we apply decltype
to a variable without any parentheses, we get
the type of that variable. If we wrap the variable’s name in one or
more sets of parentheses, the compiler will evaluate the operand as an
expression. A variable is an expression that can be the left-hand side
of an assignment. As a result, decltype
on such an expression yields
a reference:
// 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
非常简单的问题,我无法google 得出答案。
例如:
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)
添加括号使它们成为 expressions(而不是 entity),它们都是左值(作为命名变量);然后 decltype
屈服于 T&
,即这里的 int&
。
...
4) If the argument is any other expression of type
T
, and...
b) if the value category of expression is lvalue, then decltype yields
T&
;...
您可以使用此 LIVE DEMO(来自编译错误消息)检查实际类型。
Note that if the name of an object is parenthesized, it is treated as an ordinary lvalue expression, thus decltype(x) and decltype((x)) are often different types.
https://en.cppreference.com/w/cpp/language/decltype
据我了解,(x)
是一个空表达式,returns 是对 x
的引用。于是
decltype(x)
是int
declytpe((x))
是int&
根据C++ Primer:
When we apply
decltype
to a variable without any parentheses, we get the type of that variable. If we wrap the variable’s name in one or more sets of parentheses, the compiler will evaluate the operand as an expression. A variable is an expression that can be the left-hand side of an assignment. As a result,decltype
on such an expression yields a reference:// 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