1

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?

Bob Fang
  • 6,063
  • 8
  • 31
  • 64

1 Answers1

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.

Jarod42
  • 190,553
  • 13
  • 166
  • 271