[英]Why do I need to use std::move in the initialization list of a move-constructor?
Let's say I have a (trivial) class, which is move-constructible and move-assignable but not copy-constructable or copy-assignable: 假设我有一个(琐碎的)类,它是可移动构造的和可移动分配的,但不能复制构造或可复制分配的类:
class movable
{
public:
explicit movable(int) {}
movable(movable&&) {}
movable& operator=(movable&&) { return *this; }
movable(const movable&) = delete;
movable& operator=(const movable&) = delete;
};
This works fine: 这工作正常:
movable m1(movable(17));
This, of course, does not work, because m1
is not an rvalue: 当然,这是行不通的,因为
m1
不是右值:
movable m2(m1);
But, I can wrap m1
in std::move
, which casts it to an rvalue-reference, to make it work: 但是,我可以将
m1
包装在std::move
,将其强制转换为右值引用,以使其正常工作:
movable m2(std::move(m1));
So far, so good. 到现在为止还挺好。 Now, let's say I have a (equally trivial) container class, which holds a single value:
现在,让我们说我有一个(平凡的)容器类,它包含一个值:
template <typename T>
class container
{
public:
explicit container(T&& value) : value_(value) {}
private:
T value_;
};
This, however, does not work: 但是,这不起作用:
container<movable> c(movable(17));
The compiler (I've tried clang 4.0 and g++ 4.7.2) complains that I'm trying to use movable
's deleted copy-constructor in container
's initialization list. 编译器(我尝试过clang 4.0和g ++ 4.7.2)抱怨我试图在
container
的初始化列表中使用movable
的deleteed-constructor。 Again, wrapping value
in std::move
makes it work: 同样,将
value
包装在std::move
使其起作用:
explicit container(T&& value) : value_(std::move(value)) {}
But why is std::move
needed in this case? 但是为什么在这种情况下需要
std::move
? Isn't value
already of type movable&&
? value
已经不是movable&&
类型的value
了吗? How is value_(value)
different from movable m1(movable(42))
? value_(value)
与movable m1(movable(42))
有何不同?
That's because value
is a named variable, and thus an lvalue. 这是因为
value
是一个命名变量,因此是一个左值。 The std::move
is required to cast it back into an rvalue, so that it will cause move-constructor overload of T
to match. 需要
std::move
将其强制转换回右值,以使T
移动构造函数重载匹配。
To say it another way: An rvalue reference can bind to an rvalue, but it is not itself an rvalue. 换句话说:右值引用可以绑定到右值,但它本身不是右值。 It's just a reference, and in an expression it is an lvalue.
它只是一个引用,在表达式中是一个左值。 The only way to create from it an expression that is an rvalue is by casting.
从中创建右值表达式的唯一方法是强制转换。
How is
value_(value)
different frommovable m1(movable(42))
?value_(value)
与movable m1(movable(42))
有何不同?
A named rvalue reference is an lvalue (and will thus bind to the deleted copy ctor), while a temporary is, well, an rvalue (a prvalue to be specific). 命名的右值引用是左值(因此将绑定到已删除的副本ctor),而临时表是右值(具体来说是prvalue)。
§5 [expr] p6
[...] In general, the effect of this rule is that named rvalue references are treated as lvalues and unnamed rvalue references to objects are treated as xvalues [...]
[...]通常,此规则的作用是将已命名的右值引用视为左值,将对对象的未命名的右值引用视为x值[...]
Aswell as from the example: 以及从示例:
A&& ar = static_cast<A&&>(a);
The expression
ar
is an lvalue.表达式
ar
是一个左值。
The above quotes are from non-normative notes, but are an adequate explanation, since the rest of clause 5 goes and explains which expressions only create xvalues † (aka, only the specified expressions and none else will create xvalues). 上面的引文来自非规范性注释,但由于第5节的其余部分继续说明了哪些表达式仅创建xvalues † (又名,仅指定的表达式而其他都不创建xvalue),因此提供了足够的说明。 See also here for an exhaustive list.
另请参阅此处以获取详尽列表。
† xvalues are one subgroup of rvalues, with prvalues being the other subgroup. †xvalues是rvalues的一个子组,而prvalues是rvalues的另一子组。 See this question for an explanation.
请参阅此问题以获取解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.