Why do we need std::move in constructor's initialisation list?

时间:2017-08-05 10:32:13

标签: c++ c++11

Hi I am reading through Scott Meyer's Effective Modern C++ and in its item 25 it has this example:

class Widget {
    public:
    Widget(Widget&& rhs)
    : name (std::move(rhs.name)),
      p(std::move(rhs.p)
    { ... }
    ...
}

My question here is why do we need the std::move for rhs.name? As rhs is already a rvalue, doesn't that imply rhs.name is also a rvalue so we can safely move it?

1 个答案:

答案 0 :(得分:2)

As rhs has a name, it is no longer a rvalue.

You may then use

std::move(rhs.name)

or

std::move(rhs).name

to transform (cast) name into a rvalue reference.