Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An rvalue reference parameter is a function parameter declared with a double ampersand (&&) that binds exclusively to rvalues—temporary objects, literals, or objects explicitly cast to an rvalue reference. It provides a mechanism for a function to safely assume ownership of, or mutate, the internal state of an object that is guaranteed to be at the end of its lifetime.
void process(std::string&& text);

Binding Rules

An rvalue reference parameter strictly enforces value category binding during compilation:
  • Binds to prvalues (Pure Rvalues): Literals (e.g., 42) or temporary objects created by expression evaluation (e.g., std::string("temp")).
  • Binds to xvalues (eXpiring Values): Objects explicitly cast to an rvalue reference, typically via std::move().
  • Rejects lvalues: It will result in a compilation error if an attempt is made to pass a named, persistent variable (an lvalue) directly to the parameter.
void process(std::string&& text);

std::string str = "persistent";

process(std::string("temporary")); // Valid: binds to prvalue
process(std::move(str));           // Valid: binds to xvalue
process(str);                      // Error: cannot bind lvalue to rvalue reference

Internal Value Category

A critical characteristic of an rvalue reference parameter is its value category inside the function scope. Although the parameter binds to an rvalue, the parameter itself has a name. In C++, any variable with a name is an lvalue. Therefore, within the function body, the rvalue reference parameter is treated as an lvalue. If it needs to be passed to another function that also requires an rvalue reference, it must be explicitly cast back to an rvalue.
void stepTwo(std::string&& data);

void stepOne(std::string&& data) {
    // 'data' is an lvalue here because it has a name.
    
    stepTwo(data);            // Error: 'data' is an lvalue
    stepTwo(std::move(data)); // Valid: explicitly cast back to xvalue
}

Overload Resolution

Rvalue reference parameters participate heavily in overload resolution. When a function is overloaded with both an lvalue reference to const and an rvalue reference, the C++ compiler will deterministically route arguments based on their value category:
void handle(const std::string& text); // Overload 1: Binds to lvalues (and rvalues as a fallback)
void handle(std::string&& text);      // Overload 2: Binds strictly to rvalues

std::string msg = "hello";
handle(msg);             // Resolves to Overload 1
handle(std::string("")); // Resolves to Overload 2

Distinction from Forwarding References

An rvalue reference parameter requires a fully specified, concrete type (e.g., Widget&&). If the double ampersand is applied to a deduced template type parameter (e.g., template <typename T> void func(T&& param)), it ceases to be a strict rvalue reference. Instead, it becomes a forwarding reference (or universal reference), which utilizes reference collapsing rules to bind to either lvalues or rvalues depending on the argument passed.
Master C++ with Deep Grasping Methodology!Learn More