Introduced in C++11, move semantics and rvalue references (&&) are core features that dramatically improve performance, especially when dealing with large objects. By enabling low-cost transfers of resources instead of expensive copies, move semantics form one of the pillars of modern C++. This article explains what move semantics are, how they work, and where to use them—with rich, practical examples.


1) What Are Lvalues and Rvalues?

To understand move semantics, you must first understand the concepts of lvalues and rvalues.

  • lvalue: An object with a persistent memory address (e.g., variables).
  • rvalue: A temporary object, usually created as an expression result (e.g., 10, a + b, std::string("abc")).

Example:


int x = 5;        // x → lvalue
int y = x + 2;    // (x + 2) → rvalue

2) Rvalue References (&&)

The syntax && introduced in C++11 allows you to bind a reference to an rvalue (a temporary object). This opens the door to modifying or moving resources from temporary objects.


void foo(string&& s) { // rvalue reference
    cout << s << endl;
}

foo(string("Hello")); // temporary rvalue string

3) What Is Move Semantics?

Move semantics allow an object’s resources (buffers, memory blocks, file handles, etc.) to be transferred rather than copied. Copying is expensive because it duplicates data. Moving is fast because it simply transfers ownership.


string a = "Hello world";
string b = std::move(a); // transfers data from 'a' to 'b'

cout << b; // "Hello world"
cout << a; // 'a' is now in a valid but unspecified state

4) What Does std::move Actually Do?

std::move does NOT move anything by itself. It simply casts the object to an rvalue, signaling that it can be moved.


vector<int> v1 = {1,2,3,4,5};
vector<int> v2 = std::move(v1);

cout << v2.size(); // 5
cout << v1.size(); // 0 (resources transferred)

5) Move Constructor Example (Detailed)

This example class manages a dynamically allocated buffer:


class Buffer {
private:
    int* data;
    size_t size;

public:
    // Constructor
    Buffer(size_t s) : size(s) {
        data = new int[s];
    }

    // Copy Constructor
    Buffer(const Buffer& other) {
        size = other.size;
        data = new int[size];
        memcpy(data, other.data, size * sizeof(int));
    }

    // Move Constructor
    Buffer(Buffer&& other) noexcept {
        data = other.data;
        size = other.size;

        other.data = nullptr;
        other.size = 0;
    }

    ~Buffer() {
        delete[] data;
    }
};

Thanks to the move constructor, transferring large arrays becomes extremely cheap.


6) Move Assignment Operator Example


Buffer& operator=(Buffer&& other) noexcept {
    if (this != &other) {
        delete[] data;

        data = other.data;
        size = other.size;

        other.data = nullptr;
        other.size = 0;
    }
    return *this;
}

This method deletes the current object’s resources and takes over the rvalue’s resources.


7) Real-World Use Cases of Move Semantics

✔ Fast transfer of large objects and containers


vector<string> names;
names.push_back("Alice"); // rvalue → moved, no copy

✔ Returning large objects from functions


vector<int> createVector() {
    vector<int> v = {1,2,3};
    return v; // NRVO + move
}

✔ Transferring ownership with unique_ptr


unique_ptr<int> p1 = make_unique<int>(42);
unique_ptr<int> p2 = std::move(p1);

✔ Use in resource-managing classes


File file("data.txt");
File f2 = std::move(file);

8) std::forward and Perfect Forwarding

std::forward ensures that a parameter is forwarded as either an lvalue or an rvalue, depending on its original type.


template<typename T>
void wrapper(T&& x) {
    process(std::forward<T>(x)); // perfect forwarding
}

9) Things to Watch Out For When Using Move Semantics

  • A moved-from object is valid but in an unspecified state
  • std::move does not move—only casts
  • Ensure the move constructor is actually chosen by the compiler
  • Classes that manage resources should define move constructors
  • Do not reuse moved-from objects unless reinitialized

10) TL;DR

  • Rvalue → temporary object that can be moved
  • Rvalue reference (&&) → binds to rvalues
  • std::move → marks an object as movable
  • Move constructor → crucial for performance
  • Massive performance gains with large data structures
  • Foundational technique in modern C++