简体   繁体   English

为什么需要在移动构造函数的初始化列表中使用std :: move?

[英]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 from movable 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.

相关问题 为什么不调用move-constructor? - Why is the move-constructor not called? 为什么不调用移动构造函数? - Why is move-constructor not called? 如何只对move-constructor进行一次调用? - How do I make only a single call to the move-constructor? 为什么当T不可移动构造时,std :: optional的move-constructor不会被删除? - Why is the move-constructor of std::optional not deleted when T is not move-constructible? 为什么在rvalue引用构造函数的初始化列表中使用std :: forward而不是std :: move作为数据成员? - Why use std::forward rather than std::move for data member in rvalue reference constructor's initialization list? 为什么没有默认的移动分配/移动构造函数? - Why no default move-assignment/move-constructor? 为什么vector.emplace_back调用move-constructor? - why does vector.emplace_back call the move-constructor? 为什么不调用移动构造函数? (默认情况下只有构造函数,没有别的) - Why the move-constructor is not called ? (only the constuctor by default, and nothing else) 使用move-constructor时将self重置为nullptr是一个好习惯吗? - Is it a good habit to reset self to nullptr when use move-constructor? 移动构造函数和初始化列表 - Move constructor and initialization list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM