On this page
  1. Move Semantics
  2. Overview
  3. Notes & Pitfalls
  4. Example

Move Semantics

Prev: Structured Bindings | Up: builtins | Next: Concepts (C++20)
Rel: References & Rvalues, Value Categories & Move Semantics, std::unique_ptr
Ref: cppreference — Move Constructors
Tags: #public #prog #cpp #builtins #tier/standard


Overview

Move semantics (C++11) let objects transfer ownership of heap buffers or handles (O(1) pointer swap) instead of deep copying when the source is a temporary or explicitly marked movable via std::move.

Notes & Pitfalls

Example

#include <cassert>
#include <string>
#include <utility>

int main() {
    std::string a = "heap-allocated string payload";
    std::string b = std::move(a); // steals buffer without copying
    assert(b == "heap-allocated string payload");
}

Linked from

Related notes