An rvalue reference is a compound type in C++ that binds exclusively to rvalues—temporary objects, literals, or objects explicitly cast to an rvalue category (xvalues). Denoted by the 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.
&&) syntax, it provides a language-level mechanism to safely identify, bind to, and mutate temporary objects before their memory is reclaimed.
Binding Rules and Value Categories
In the C++ expression taxonomy, an rvalue reference can only bind to expressions that evaluate to an rvalue (specifically, prvalues and xvalues). It strictly prohibits binding to lvalues (named objects with a persistent memory address) unless an explicit cast is applied.- prvalue (Pure Rvalue): Literals or temporary objects returned by value.
- xvalue (Expiring Value): Objects nearing the end of their lifetime, typically created via an explicit cast to an rvalue reference.
Overload Resolution
Rvalue references participate heavily in function overload resolution. When the compiler encounters an overloaded function, it will preferentially bind rvalue arguments to the overload taking an rvalue reference (T&&), and lvalue arguments to the overload taking an lvalue reference (T& or const T&).
The Lvalue-ness of Named Rvalue References
A critical mechanical rule in C++ is that if an rvalue reference has a name, the name itself is treated as an lvalue expression. The type of the variable isT&&, but its value category when evaluated in an expression is an lvalue. This prevents temporary objects from being implicitly cannibalized multiple times.
Disambiguation: Forwarding References
When the&& syntax is applied to a deduced template type parameter or auto, it does not strictly denote an rvalue reference. Instead, it becomes a forwarding reference (sometimes called a universal reference).
& & -> &, & && -> &, && & -> &, && && -> &&), a forwarding reference can collapse into either an lvalue reference or an rvalue reference depending on the value category of the argument passed to it. A true rvalue reference requires a fully specified, non-deduced type (e.g., std::string&& or int&&).
Master C++ with Deep Grasping Methodology!Learn More





