On this page
References & Rvalues (& / &&)
Prev: constexpr & consteval | Up: builtins | Next: RAII & Destructors
Rel: Move Semantics, Value Categories & Move Semantics, nullptr
Ref: cppreference — Reference Declaration
Tags: #public #prog #cpp #builtins #tier/core
Overview
A C++ lvalue reference (T&) is a non-null, non-reseatable alias to an existing object. An rvalue reference (T&&) binds to temporaries or explicitly moved objects, enabling move constructors and move assignment.
Reference Kinds
| Syntax | Binds To | Typical Use |
|---|---|---|
T& |
Mutable lvalues | Out-parameters, mutating existing objects |
const T& |
Lvalues and temporaries (rvalues) | Read-only borrowing without copying |
T&& |
Rvalues (temporaries, std::move(x)) |
Move constructors, sinks that steal resources |
T&& (deduced template param) |
Any value category (forwarding reference) | Perfect forwarding via std::forward<T>(x) |
Gotchas
- Never return a reference to a local stack variable—it becomes a dangling reference immediately on function exit.
- Use pointers (
T*) orstd::optionalwhen "absent / nullable" is part of the contract; use references when an object must exist.
Linked from
Related notes
- tuts/cpp/builtins/constexpr Linked here; links to this page; shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/move_semantics Linked here; links to this page; shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/nullptr Linked here; links to this page; shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/raii_and_destructors Linked here; links to this page; shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins Linked here; links to this page; shared tags: #builtins, #cpp, #prog
- tuts/cpp/language_and_raii/value_categories_and_move_semantics Linked here; links to this page; shared tags: #cpp, #prog, #tier
- tuts/cpp/builtins/auto links to this page; shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/c_style_casts shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/casts shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/classes_and_virtual shared tags: #builtins, #cpp, #prog, #tier