Why do I need to use std::move in the initialization list of a move-constructor?

前端 未结 2 1566
小鲜肉
小鲜肉 2021-01-01 22:21

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:         


        
2条回答
  •  春和景丽
    2021-01-01 22:28

    That's because value is a named variable, and thus an lvalue. The std::move is required to cast it back into an rvalue, so that it will cause move-constructor overload of T to match.

    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.

提交回复
热议问题