Question about std::move

Rothbard used Ask the Experts™
I already know some C++98 and am trying to familiarize myself with C++11. I am struggling to figure out how std::move works. My understanding, e.g. from here is that this works as a static_cast to an rvalue reference, in that it returns

static_cast<typename std::remove_reference<T>::type&&>(t) 

Open in new window

However, I find this puzzling since my understanding of std::remove_reference is that it actually does the opposite, i.e. it changes T& and T&& into T, since it can be implemented as

template <class T> struct remove_ref { typedef T type; };
template <class T> struct remove_ref<T&> { typedef T type; };
template <class T> struct remove_ref<T&&> { typedef T type; };

Open in new window

(see here). So how is it possible for std::move to act as a static_cast to an rvalue reference type?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
evilrixSenior Software Engineer (Avast)

Literally, all std::move does is change the value into an r-value reference. This tells the compiler it is a candidate for being movable. It really is as simple as that. So basically, it turns T to T && and that's enough to make it movable.

Have you taken a look at the implementation of std::move? It's pretty simple and should be obvious how it works. Remember, all that's happening here is the type is being cast to another type. Essentially, just casting the type to an r-value type that is a candidate for move semantics to be applied.


Thanks, I understand now.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial