Skip to content
Beyond Coordinates
Go back

Understanding C++ Rvalue References and std::move

Updated:

C++11 introduced rvalue references and std::move, two key features that significantly improve resource management in C++, particularly through move semantics and perfect forwarding.

Rvalue References

In C++, expressions can be categorized as lvalues and rvalues:

An rvalue reference is declared with &&:

int &&r = 10;  // rvalue reference

Rvalue references make it possible to take ownership of an object’s resources without an expensive copy. This enables move semantics, in which resources are transferred from one object to another.

Important properties of rvalue references include:

std::move

std::move casts its argument to an rvalue reference. It does not move anything by itself; it tells the compiler that an lvalue may be treated as an rvalue, allowing move operations to participate in overload resolution.

T&& std::move(T& x);

The cast allows a move constructor or move-assignment operator to be selected.

#include <iostream>
#include <vector>

class MyClass {
public:
    MyClass() { std::cout << "MyClass created!" << std::endl; }
    MyClass(const MyClass&) { std::cout << "MyClass copied!" << std::endl; }
    MyClass(MyClass&&) { std::cout << "MyClass moved!" << std::endl; }
};

int main() {
    MyClass obj1;  // Create an object.
    MyClass obj2 = std::move(obj1);  // Move from obj1 into obj2.
}

In this example:

Move Semantics

Move semantics transfer an object’s resources instead of copying them. Copying a large container or dynamically allocated buffer can be expensive, especially when objects are repeatedly passed to and returned from functions. Moving can eliminate much of that cost.

The move constructor and move-assignment operator are the two core operations.

Example move constructor:

class MyClass {
public:
    MyClass() { std::cout << "MyClass created!" << std::endl; }
    MyClass(const MyClass&) { std::cout << "MyClass copied!" << std::endl; }
    MyClass(MyClass&& other) {
        std::cout << "MyClass moved!" << std::endl;
        // Transfer resources from other.
    }
};

A move constructor typically takes ownership of the source object’s resources and leaves the source in a valid but otherwise unspecified state.

Example move-assignment operator:

MyClass& operator=(MyClass&& other) {
    std::cout << "Move assignment!" << std::endl;
    // Release the current resources and transfer resources from other.
    return *this;
}

Use Cases

Summary

Rvalue references commonly appear in function parameters. Such a parameter can bind directly to an rvalue or to an lvalue explicitly cast with std::move. Used correctly, rvalue references and std::move make C++ programs more efficient, especially in code that creates, transfers, and destroys many resource-owning objects.


Share this post:

Previous Post
WebAssembly Study Notes
Next Post
Notes on Natural Language Processing