A sample implementation of std::move
, which better be memorized as rvalue_cast
template <typename T>
decltype(auto) move(T&& param) {
return static_cast<std::remove_reference_t<T>&&>(param);
}
Don’t declare objects const
if you want to be able to move from them
#include <string>
struct S {
S(const std::string text)
: value(std::move(text)) {
}
std::string value;
}
std::string
was copied, not moved. The move constructor takes an rvalue reference to a non-const std::string
, and std::move(text)
is a const std::string
, it can be passed to the copy constructor, because an lvalue-reference-to-const is permitt