liveBook · Manning

Rvalue reference

As explained in 100 C++ Mistakes and How to Avoid Them

An rvalue reference is a C++ reference type that can bind to temporary or otherwise movable expressions and enable resource transfer.

An rvalue reference uses && to refer to a right-hand-side value, including a temporary, so its resources can be transferred instead of copied. section 2.1 section 2.0

Mistake: copying a uniquely owned object and then repairing ownership manually. The copy constructor duplicates data before deleting the source data, creating unnecessary cost and fragile invalidation rules. section 2.1

Listing 2.1 A uniqueness-preserving assignment operator with heavy cost
class TextSection {
    // assume a clever implementation
};
 
class Page {
private:
    TextSection* headers;
    TextSection* body;
public:
    Page(TextSection* h) : headers(h), body(new TextSection()) {}
    Page(Page& o);
    ~Page() { if (body) delete body; }
};
 
Page::Page(Page& o) {
    if (this == &o)
        return;
    body = new TextSection(*o.body);
    delete o.body;
    o.body = 0;
    headers = o.headers;
    o.headers = 0;
}
 
int main() {
    Page p1(new TextSection());
    Page p2 = p1;                     #1
    return 0;
}

The problematic Page example copies body, deletes the source body, and nulls the source pointers; extensive data makes the duplication expensive. section 2.1

Modernize the transfer with a move constructor or move assignment operator. The destination takes ownership, while the source is invalidated so its former data cannot be accessed. section 2.1

Listing 2.2 A uniqueness-preserving assignment operator with negligible cost
class TextSection {
    // assume a clever implementation
};
 
class Page {
private:
    TextSection* headers;
    TextSection* body;
public:
    Page(TextSection* h) : headers(h), body(new TextSection()) {}
    Page(const Page& o) : headers(o.headers), body(new            #1
TextSection(*o.body)) {}
    Page(Page&& o) : headers(o.headers), body(o.body) {           #2
         o.headers = nullptr;                                     #3
         o.body = nullptr;
    }
    ~Page() { delete body; }
};
 
int main() {
    Page p1(new TextSection());
    Page p2 = std::move(p1);                                      #4
    return 0;
}

The corrected Page example transfers pointers through Page(Page&&) and calls std::move(p1), avoiding data duplication and nulling the moved-from source. section 2.1

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Defines &&, rvalues, move constructors, and source invalidation through contrasting Page implementations, without broader language internals.
Examples
Uses Page and TextSection examples to contrast expensive copying with pointer transfer through Page(Page&&) and std::move.
What it assumes
Assumes familiarity with C++ classes, constructors, pointers, and ownership while teaching rvalue references and move semantics directly.
What you can do after
Apply it via move constructors and move assignment operators to transfer resources while invalidating moved-from objects.
How it connects
Connects rvalue references with move semantics, move constructors, move assignment operators, copy constructors, and exclusive ownership.

Also explained in

Compare how 2 books explain rvalue reference →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.