An rvalue reference parameter is a function parameter declared with a double ampersand (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.
&&) 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.
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.
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.Overload Resolution
Rvalue reference parameters participate heavily in overload resolution. When a function is overloaded with both an lvalue reference toconst and an rvalue reference, the C++ compiler will deterministically route arguments based on their value category:
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





