Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Value categories and move semantics

The categories lvalue, prvalue, and xvalue describe expressions, not kinds of variables. Learn their glvalue and rvalue groupings and how they relate to references, overload selection, and std::move, which does not itself transfer resources.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

lvalue: x; prvalue: 42; xvalue: std::move(x); glvalue = lvalue | xvalue; rvalue = prvalue | xvalue

Every expression has exactly one fundamental value category: lvalue, prvalue, or xvalue. An lvalue identifies an object or function and is not an xvalue; a prvalue computes a value or initializes an object; an xvalue identifies an object whose resources can be reused. glvalue includes lvalue and xvalue, while rvalue includes prvalue and xvalue. The category belongs to an expression, not a variable's type; an lvalue need not be modifiable.

An expression x naming an ordinary variable is usually an lvalue. In C++23, if it names a local variable and is move-eligible as the operand of a return statement, it is an xvalue. The expression 42 is a prvalue, and std::move(x) for an object is an xvalue. A declaration type of T&& does not make every use of the name an xvalue: in the example, rr is an lvalue. When a temporary object is needed, temporary materialization can convert a prvalue into an xvalue denoting that object.

std::move from <utility> casts to an rvalue reference; it does not itself copy or transfer resources. It enables selection of a move constructor, usually T(T&&), instead of a copy constructor T(const T&). The actual action depends on the selected overload. Copying can still be selected; std::move preserves const, so the usual T(T&&) constructor cannot accept a const object. The source's state after a move depends on the type's contract—do not assume the object is empty.

The example separates reference binding from construction: initializing rr prints nothing, b(rr) copies, and c(std::move(rr)) selects the move constructor. The static_assert declarations check categories through decltype; the parentheses in decltype((rr)) matter. The constructors only print messages and manage no resource. This entry targets C++23; the supplied standard URL is an evolving working draft, not a frozen C++23 publication.

Examples

#include <iostream>
#include <type_traits>
#include <utility>

struct Item {
    Item() = default;
    Item(const Item&) { std::cout << "copy\n"; }
    Item(Item&&) noexcept { std::cout << "move\n"; }
};

int main() {
    Item a;
    Item&& rr = std::move(a);

    static_assert(std::is_same_v<decltype((a)), Item&>);
    static_assert(std::is_same_v<decltype((rr)), Item&>);
    static_assert(std::is_same_v<decltype(std::move(a)), Item&&>);
    static_assert(std::is_same_v<decltype(Item{}), Item>);

    [[maybe_unused]] Item b(rr);
    [[maybe_unused]] Item c(std::move(rr));
}

Result

copy
move

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources