skip to content

Value Categories

Every expression is an lvalue, xvalue or prvalue, and that category picks the overload a call binds to. Interviewers ask what std::move really does and why a named rvalue reference is an lvalue.

on this pageshow

explore

questions

5

In C++, what does std::move actually do to the object you pass to it?

level: juniorimportance: must knowfreq 85%

answer

  1. a function that moves nothing
  2. a cast to an rvalue reference
  3. the result is an xvalue
  4. the receiving constructor does the work

basics

~20 s

std::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
cpp
#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

for a junior

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.

for a middle

Explain the overload resolution step: the xvalue makes the T&& overload preferable to const T&, and without such an overload the call copies.

for a senior

Show where the cast silently fails to move: a missing move constructor, a const argument, a const T& receiver, a trivially copyable type.

for a principal

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.
open as a page

In C++, given overloads f(const std::string&) and f(std::string&&), which one does each kind of argument select, and why?

level: middleimportance: must knowfreq 65%

basics

~20 s

An lvalue can bind only to const std::string&, so it selects that overload. An rvalue - a prvalue like make() or an xvalue like std::move(s) - can bind to both, and overload resolution prefers std::string&&. A const rvalue falls back to const std::string&.

open as a page

A code review asks why void Registry::add(std::string&& name) { names_.push_back(name); } still copies the string — what is the answer?

level: seniorimportance: must knowfreq 60%

basics

~10 s

Inside add, the expression name is an lvalue because it is a name, even though its declared type is std::string&&. So push_back(name) selects push_back(const std::string&) and copies; push_back(std::move(name)) restores the rvalue and moves.

open as a page

In C++, what distinguishes an lvalue, an xvalue and a prvalue, and which category does std::move(s) produce?

level: middleimportance: should knowfreq 55%

basics

~20 s

Every C++ expression is exactly one of three: an lvalue names an object you keep using, an xvalue denotes an object whose resources may be reused, and a prvalue computes a value or initializes an object. std::move(s) is an xvalue.

open as a page

In C++, why does v.push_back(std::move(cfg)) compile without complaint yet copy when cfg is a const std::string?

level: seniorimportance: should knowfreq 40%

basics

~10 s

std::move(cfg) produces a const std::string&& xvalue. push_back(std::string&&) cannot bind it, because that would drop const, so overload resolution falls back to push_back(const std::string&), which binds anything and copies. No error is required.

open as a page