Rvalue References and the && Syntax
Bind to temporaries with rvalue references for efficient resource transfer.
Rvalue References and the && Syntax is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The && Syntax
An rvalue reference is declared with T&&. It binds only to rvalues — temporaries about to die.
int&& ref = 42; // OK: binds to literal
int x = 10;
// int&& bad = x; // ERROR: x is an lvalueWhy Have rvalue References?
They let you write functions that know the argument is about to be destroyed. You can take its resources without making a copy.
Function Overloads on Reference Type
You can overload a function on T& and T&&. The compiler picks the right one based on the argument s category.
void process(std::string& s) { std::cout << "lvalue\n"; }
void process(std::string&& s) { std::cout << "rvalue\n"; }
std::string s = "hi";
process(s); // lvalue
process("temp"); // rvalue
process(std::move(s)); // rvalueNamed Rvalue Refs Become Lvalues
Once you give an rvalue reference a name, that name is itself an lvalue — its identity has been pinned down. Use std::move to recover rvalue-ness.
void f(std::string&& s) {
// inside f, the parameter s is an lvalue
g(s); // passes s by lvalue
h(std::move(s)); // passes s as an rvalue
}std::move Is a Cast
std::move(x) does not move anything by itself — it is a type cast that returns x as an rvalue reference. The actual move happens in the receiving constructor.
// equivalent to
static_cast<std::string&&>(s)Why It Is Called Move
The name reflects the typical usage — passing an object whose contents you intend to steal — but it does not transfer anything on its own.
Universal References (Forwarding References)
When T&& appears in a template with deduced T, it is a forwarding reference — it binds to lvalues and rvalues both, preserving the category.
template <typename T>
void wrapper(T&& arg) {
process(std::forward<T>(arg));
}Reference Collapsing
When references stack via templates, the language collapses them:
T& &→T&T& &&→T&T&& &→T&T&& &&→T&&
When Not to Use Move
For trivially copyable types (int, double, pointer), move is the same as copy. Calling std::move adds no benefit and may confuse readers.
Move Without Owning Resources
For types without heap resources, the move constructor is the same as the copy constructor. Move is only beneficial for types that own dynamic memory.
Common Pitfall: Moving from const
std::move on a const object is a no-op — the move constructor cannot be called on a const because it would need to modify the source.
Quick Check
What does std::move(x) actually do?
Recap
T&& binds to rvalues. Named rvalue refs become lvalues — use std::move to recover. T&& in a template is a forwarding reference. std::move is a cast; it does not perform the move itself.
Frequently asked questions
Is the “Rvalue References and the && Syntax” lesson free?
Yes — the full text of “Rvalue References and the && Syntax” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Rvalue References and the && Syntax”?
Bind to temporaries with rvalue references for efficient resource transfer. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Rvalue References and the && Syntax” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Lvalue vs Rvalue The Distinction
- Rvalue References and the && Syntax
- std::move and std::forward
- Move Constructors and Move Assignment