J
Juha Nieminen
Suppose you have a function that takes an rvalue reference as parameter,
and from within it you want to call another function that likewise takes
one, but there's also a version of that function that takes a regular
reference. (This is most often the case with copy/move constructors and
assignment operators.)
In order to call the version taking the rvalue reference, you have to
explicitly re-cast the parameter with std::move (or static_cast<type&&>()).
For example, if you were writing a move constructor in a derived class,
you would need to write it like:
MyClass(MyClass&& rhs): BaseClass(std::move(rhs)) {}
Or, if you want:
MyClass(MyClass&& rhs): BaseClass(static_cast<MyClass&&>(rhs)) {}
Without the explicit cast, the normal copy constructor of the base class
would be called instead. (The same is true if you were implementing the
move assignment operator.)
But why is the explicit re-casting necessary?
and from within it you want to call another function that likewise takes
one, but there's also a version of that function that takes a regular
reference. (This is most often the case with copy/move constructors and
assignment operators.)
In order to call the version taking the rvalue reference, you have to
explicitly re-cast the parameter with std::move (or static_cast<type&&>()).
For example, if you were writing a move constructor in a derived class,
you would need to write it like:
MyClass(MyClass&& rhs): BaseClass(std::move(rhs)) {}
Or, if you want:
MyClass(MyClass&& rhs): BaseClass(static_cast<MyClass&&>(rhs)) {}
Without the explicit cast, the normal copy constructor of the base class
would be called instead. (The same is true if you were implementing the
move assignment operator.)
But why is the explicit re-casting necessary?