CKTIS / C++
Rvalue reference declarator (`&&`)
The `&&` declarator forms an rvalue reference type. Overload resolution still depends on expression category: the name of a variable declared `T&&` is an lvalue when passed as an argument.
Version: ISO C++23; implementation support may vary
Syntax
T&& ref = expr; // local reference; requires a suitable initializer
void consume(T&& arg); // reference parameter
T&& access(); // reference return type; does not extend lifetime
using R = T&&; // reference type alias; collapsing can apply
template<class U> void relay(U&& arg); // forwarding reference when U is deducedIn T&& ref, when T is a non-reference object type, the rvalue reference declarator (&&) declares an rvalue reference. T& is an lvalue reference, and in const T&, const qualifies the object. This is not built-in logical a && b, which skips the right operand when the left is false, nor bitwise a & b or address-of &x. In void f() &&, the token is a member-function ref-qualifier, not a reference declarator. Rvalue references date to C++11; this entry uses ISO C++23 rules.
For the same object type T, an ordinary T&& cannot bind directly to an lvalue of type T. It can bind to a prvalue, which materializes a temporary for the binding, or to an xvalue such as std::move(x), which can designate an existing object. As a call argument, the name ref is an lvalue even if declared T&&. With the example's overloads taking const int& and int&&, an int rvalue selects int&&, while an int lvalue or a const int xvalue selects const int&.
For an object, std::move from <utility> produces an xvalue: it does not remove const or move anything by itself. Task(Task&&) and Task& operator=(Task&&) are typical move operations; Task& in the second declaration is the return type. The operation can transfer ownership of a resource such as a coroutine frame, but binding the reference does not. On copy operations, = delete forbids their use; = default on a move operation requests the language-defined implementation, which can still be deleted.
A local declaration T&& ref = T{}; extends that temporary's lifetime to the lifetime of the reference. A temporary bound to a reference parameter lasts only until the end of the full-expression containing the call. Passing the reference on does not extend the lifetime again. Do not return a reference to a local object that is destroyed when the function exits.
In template<class U> void relay(U&& arg), deduced U&& is a forwarding reference. For an lvalue of type T, U is deduced as T& and reference collapsing yields T&; for an rvalue of type T, the result is T&&. When references collapse, any & makes the result &; only two && yield &&. Use std::forward<U>(arg) from <utility> to pass the argument on as an lvalue or rvalue according to deduction. const U&& is not a forwarding reference.
Examples
#include <iostream>
#include <utility>
void probe(const int&) { std::cout << "const int&\n"; }
void probe(int&&) { std::cout << "int&&\n"; }
int main() {
int value = 1;
int&& ref = 2;
const int frozen = 3;
probe(value);
probe(7);
probe(ref);
probe(std::move(ref));
probe(std::move(frozen));
}
Result
const int& int&& const int& int&& const int&
In the call, `ref` is an lvalue, while `std::move(ref)` is an xvalue and selects `int&&`. `std::move(frozen)` preserves `const`, so it selects `const int&`. The program demonstrates overload selection, not resource transfer.
Related lessons
- C++ · Lesson 11 · Advanced · Version: 3
- C++ · Lesson 19 · Advanced · Version: 3
- C++ · Lesson 41 · Advanced · Version: 3
- C++ · Lesson 42 · Advanced · Version: 3
- C++ · Lesson 43 · Advanced · Version: 3
- C++ · Lesson 45 · Advanced · Version: 3
Sources
- Working Draft, Standard for Programming Language C++ (N4950)
- References [dcl.ref]
- Reference initialization [dcl.init.ref]
- Value category [basic.lval]
- Temporary objects [class.temporary]
- Deducing template arguments from a function call [temp.deduct.call]
- Forward/move helpers [forward]
- Ranking implicit conversion sequences [over.ics.rank]
- Copy/move constructors [class.copy.ctor]
- Copy/move assignment operator [class.copy.assign]
- Compound expressions [expr.compound]
- Functions [dcl.fct]