In C++, what does std::move actually do to the object you pass to it?
answer
- a function that moves nothing
- a cast to an rvalue reference
- the result is an xvalue
- the receiving constructor does the work
basics
~20 sstd::move moves nothing: it is a cast that returns its argument as an xvalue, static_cast to an rvalue reference. A move happens only if that xvalue then selects a move constructor or move assignment operator, which transfers the resources.
solid answer
~40 s`std::move` is declared as `template<class T> constexpr remove_reference_t<T>&& move(T&& t) noexcept;` and the standard specifies its result as `static_cast<remove_reference_t<T>&&>(t)`. It touches nothing inside the object; it only turns an lvalue expression into an xvalue, so overload resolution can prefer an overload taking `T&&`. The transfer is done by whoever receives that xvalue: in `std::string t = std::move(s);` it is `std::string`'s move constructor. If nothing receives it, nothing happens - `std::move(s);` alone leaves `s` as it was, and both libc++ and libstdc++ mark `std::move` `[[nodiscard]]`, so Clang warns about the discarded result. And if the type has no usable move overload, the xvalue binds to `const T&` and the "move" is a copy.
code
cpp · 11 lines#include <iostream>
#include <string>
#include <utility>
int main() {
std::string s = "level-01.map";
std::move(s); // a cast whose result is discarded
std::cout << s << '\n'; // still "level-01.map": nothing was moved
std::string t = std::move(s); // the move constructor does the work
std::cout << t << '\n';
}go deeper
Say it plainly: std::move is a cast to an rvalue reference, and the move constructor or move assignment that receives it does the actual transfer.
Explain the overload resolution step: the xvalue makes the T&& overload preferable to const T&, and without such an overload the call copies.
Show where the cast silently fails to move: a missing move constructor, a const argument, a const T& receiver, a trivially copyable type.
Frame std::move as a statement of intent about the caller's object, and expect reviews to check that intent against the receiver's actual overloads.
## The declaration says it all `std::move` lives in `<utility>`. The C++ standard declares it as ```cpp template<class T> constexpr remove_reference_t<T>&& move(T&& t) noexcept; ``` and specifies its result as `static_cast<remove_reference_t<T>&&>(t)`. That is the whole function. It allocates nothing, frees nothing, and never reads or writes the object's members. Its single effect is on the **value category** of the expression: the name `s` is an **lvalue** (an expression that denotes an object you can keep using), while `std::move(s)` is an **xvalue** (an expression that denotes an object whose resources may be reused). The standard's own list of xvalues includes exactly this case: a cast to an rvalue reference to an object type. A better name would have been `rvalue_cast`. The function is `noexcept` because a cast cannot fail, and `constexpr` because a cast can be evaluated at compile time. ## Where the move really happens A move is a member function call, not a property of `std::move`. For `std::string t = std::move(s);` the steps are: 1. `std::move(s)` produces an xvalue of type `std::string`. 2. Initializing `t` performs overload resolution over `std::string`'s constructors. 3. Both the copy constructor (`const std::string&`) and the move constructor (`std::string&&`) can accept an xvalue, and the language prefers binding an rvalue reference to an rvalue, so the move constructor wins. 4. The move constructor runs. It takes over the source's heap buffer, if any, and leaves the source in a valid but unspecified state. Step 4 is the move. Steps 1 to 3 only decided that it would happen. ## A bare std::move does nothing Because `std::move` is a cast, calling it and discarding the result changes nothing: ```cpp std::string s = "level-01.map"; std::move(s); // the xvalue is discarded std::cout << s; // still prints level-01.map ``` The standard does not mark `std::move` `[[nodiscard]]`, but libc++ and libstdc++ both declare it with that attribute, and Clang warns about the discarded result even without `-Wall`. ## When the cast does not lead to a move The cast only *permits* a move. Whether one happens depends on the receiver: - **The type has no move constructor.** A class with a user-declared copy constructor gets no implicitly declared move constructor, so the xvalue binds to `const T&` and the copy constructor runs. The code compiles and silently copies. - **The receiver takes `const T&` only.** A function `void log(const std::string&)` called as `log(std::move(s))` binds a reference and copies or moves nothing. - **The argument is `const`.** `std::move` on a `const std::string` yields a `const std::string&&`, which the move constructor cannot accept, so the copy constructor runs. - **The type is trivially copyable.** For an `int` or a plain struct of `int`s, moving and copying are the same operation; `std::move` is harmless but pointless. | Expression | What runs | Source afterwards | |---|---|---| | `std::string t = s;` | copy constructor | unchanged | | `std::string t = std::move(s);` | move constructor | valid but unspecified | | `std::move(s);` | nothing | unchanged | | `log(std::move(s));` with `log(const std::string&)` | nothing (reference binding) | unchanged | ## Why the design is a cast C++ decides between copying and moving by **overload resolution on the value category** of the argument. Temporaries are rvalues, so they are moved from automatically: nobody else can observe them. A named variable is an lvalue, and the language will not move from it on its own, because the program might read it again. `std::move` is how the programmer says "I am done with this object; treat it like a temporary". Keeping that statement a pure cast means the decision about *how* to move stays with the type's author, who writes the move constructor and move assignment operator, and the decision about *whether* the source is still needed stays with the caller, who writes the cast. That split explains the interview answer: `std::move` grants permission, and the move operations do the moving. ## Quick checks in code review - **`std::move` on something that is already an rvalue** - `std::move(make())` - is redundant: the prvalue would already select the move overload. - **`std::move` on a variable that is read again later** is a bug in waiting: the later read sees a valid but unspecified value. - **`std::move` passed to a `const T&` parameter** documents an intent the callee never acts on; either the callee should gain a `T&&` overload or the cast should go. - **`std::move` in a template on a parameter declared `T&&`** usually wants `std::forward<T>` instead, because that parameter may have been bound to the caller's lvalue - a topic of its own under perfect forwarding. None of these is diagnosed by the compiler; each is a reading skill that follows from knowing that the function is only a cast.
- What happens when you std::move an object whose class declares a copy constructor but no move constructor?A user-declared copy constructor suppresses the implicitly declared move constructor, so overload resolution finds only the copy constructor. Its `const T&` parameter binds to the xvalue, and the object is copied. The code compiles without a diagnostic. An explicitly deleted move constructor behaves differently: a deleted function still takes part in overload resolution, is selected, and the call is an error.
- How would you write your own equivalent of std::move, and why does it need remove_reference_t?`template<class T> constexpr std::remove_reference_t<T>&& my_move(T&& t) noexcept { return static_cast<std::remove_reference_t<T>&&>(t); }`. For an lvalue argument `T` is deduced as `U&`, and `T&&` would collapse back to `U&`, an lvalue reference. Stripping the reference first guarantees the return type is `U&&`, so the call is always an xvalue.
- std::move(x) on its own line transfers x's resources somewhere.
- After std::move(x), x is guaranteed to be empty.
- std::move performs a shallow bitwise copy and nulls the source.
- Wrapping an argument in std::move always makes the call cheaper.
- std::move forces a move even when the type has no move constructor.