- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设 m
是非引用类型 (T
) 的非静态数据成员。根据cppreference , std::move(a).m
在 c++11 之前是纯右值。我猜它应该是c++11之后的xvalue。如果我错了,请纠正我。
但是在 c++14 中,decltype(std::move(a).m)
仍然是 T
(不是 T&&
) (Visual Studio、Clang、GCC),这表明 std::move(a).m
仍然是纯右值。那么 std::move(a).m
是 xvalue 还是纯右值?
最佳答案
std::move(a).m
是一个 xvalue。
[basic.lval] 中的新措辞使这一点更加清晰:
- A prvalue is an expression whose evaluation initializes an object or a bit-field, or computes the value of the operand of an operator, as specified by the context in which it appears.
- An xvalue is a glvalue that denotes an object or bit-field whose resources can be reused (usually because it is near the end of its lifetime).
根据这些定义,std::move(a).m
是一个 xvalue,而不是纯右值,因为它表示一个对象。
我认为最好的思考方式是左值具有同一性并且右值可以安全地移动 - 左值具有同一性并且不能安全地移动,x值具有同一性并且可以安全地移动,而纯右值可以安全地移动。没有身份并且可以安全地离开。这种分类法使此类问题更容易推理。
此外[expr]中有一个注释,更具体:
[ Note: An expression is an xvalue if it is: [...]
— a cast to an rvalue reference to object type,
— a class member access expression designating a non-static data member of non-reference type in which the object expression is an xvalue, or [...]
—end note ]
std::move(a)
是对右值引用的强制转换,xvalue 也是如此。 std::move(a).m
是 xvalue 的类成员访问,xvalue 也是如此。
至于decltype(std::move(a).m)
。请注意,该词本身来自已声明类型。 decltype(e)
含义的规则很复杂,来自 [dcl.type.simple]:
For an expression
e
, the type denoted bydecltype(e)
is defined as follows:
— ife
is an unparenthesized id-expression naming an lvalue or reference introduced from the identifier-list of a decomposition declaration,decltype(e)
is the referenced type as given in the specification of the decomposition declaration (8.5);
— otherwise, ife
is an unparenthesized id-expression or an unparenthesized class member access (5.2.5),decltype(e)
is the type of the entity named bye
. If there is no such entity, or ife
names a set of overloaded functions, the program is ill-formed;
— otherwise, ife
is an xvalue,decltype(e)
isT&&
, whereT
is the type ofe
;
— otherwise, ife
is an lvalue,decltype(e)
isT&
, whereT
is the type ofe
;
— otherwise,decltype(e)
is the type ofe
.
在本例中,我们拥有类成员访问权限,因此您只需获取 m
的类型 - 即 M
而不是 M&&
。在某种程度上,这是有道理的,您要求 m
的声明类型,并且您得到了 m
的声明类型。
如果你想对其进行正确分类,你可以使用一组额外的括号强制忽略该项目符号(显然):decltype((std::move(a).m))
会给你M&&
。
关于c++11 - std::move(a).m 是 xvalue 还是纯右值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42026327/
我是一名优秀的程序员,十分优秀!