Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::move — a cast that enables moving

std::move from <utility> exposes an object as an xvalue, enabling selection of a move operation. The call itself neither transfers resources nor empties the object; the actual effect depends on the operation that consumes its result.

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

Syntax

#include <utility>

template<class T>
constexpr std::remove_reference_t<T>&& move(T&& t) noexcept;

// Usage:
std::move(expression)

std::move from <utility> returns static_cast<std::remove_reference_t<T>&&>(t): an rvalue reference to the same object. The result is an xvalue expression. The cast itself does not construct an object, transfer resources, or end the argument's lifetime.

Only an operation consuming the result, such as a constructor or assignment operator, can transfer resources. Moving is not guaranteed: depending on the available overloads, copying or a compilation error may result. std::move preserves const, so an ordinary constructor taking T&& cannot accept the result for a const T object.

After an actual move, standard-library objects remain in a valid but unspecified state unless the type's specification guarantees more. Do not assume they are empty; respect the preconditions of subsequent operations. User-defined types need their own contract for the moved-from state.

In the example, the move constructor of std::unique_ptr transfers ownership to destination and guarantees that source holds a null pointer. Thus the output true and 42 is specified; dereferencing source afterward is not allowed. This entry targets C++23; the supplied working draft is updated over time and is not a frozen C++23 edition.

Examples

#include <iostream>
#include <memory>
#include <utility>

int main() {
    auto source = std::make_unique<int>(42);
    auto destination = std::move(source);

    std::cout << std::boolalpha
              << (source == nullptr) << '\n'
              << *destination << '\n';
}

Result

true
42

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