Lvalue References¶
An lvalue reference (T&) is an alias for an existing object. It is not a
pointer with nicer syntax, and it is not a separate object with its own address.
Understanding what it binds to, what it cannot do, and when it dangles is the
difference between a clean interface and a lifetime bug that survives review.
1. Type and value category are different questions¶
int& is a type. "Lvalue" and "rvalue" describe expressions. The two
questions are related but not identical:
- An lvalue expression names an object or a function.
- An rvalue expression is a temporary value or an expiring object.
T&is an lvalue reference type;T&&is an rvalue reference type, except whenTis deduced in a forwarding-reference context.
This is why T&& in a template is not automatically an rvalue reference:
The next article in this track covers forwarding references and
std::forward; this page stays with T&.
2. What T& can bind to¶
A non-const lvalue reference binds to an lvalue. A const lvalue reference can
also bind to an rvalue, which is how const T& accepts both named objects and
temporaries:
int x = 10;
int& r1 = x; // binds to the object x
const int& r2 = 42; // binds to a temporary; its lifetime is extended
// int& r3 = 42; // error: a non-const lvalue reference cannot bind to an rvalue
The binding rules are in [dcl.init.ref] and [dcl.ref]. The lifetime
extension for a temporary bound directly to a reference is in
[class.temporary].
A reference can bind to an object reached through an expression, not only to a bare variable:
int& r = array[0];int& r = object.member;int& r = *pointer;(the pointer must be valid)
The reference does not keep the object alive. It only names it.
3. A reference cannot be rebound, null, or used for arithmetic¶
Once initialized, a reference always refers to the same object. Assignment through the reference changes the object; it does not make the reference point somewhere else:
A reference must be initialized, cannot be null, and has no ++ or --
operation of its own. sizeof(T&) is sizeof(T), because the reference is not
an object in the language's object model. These properties make references good
for "this parameter must name an object" APIs, but bad for "may be absent" or
"may be reseated" APIs.
Use a pointer for optional or reseatable access; use a smart pointer when the API also owns the object.
4. Lifetime is the part that bites¶
A reference is only as valid as the object it names. Returning a reference to a local is the canonical dangling-reference bug:
The compiler may warn, but the type system does not carry the lifetime. The same
problem appears with references to elements of a container that reallocates,
references to members of a temporary, and string_view objects that outlive
their owning string.
A const T& lifetime extension is narrow: it applies when a temporary is bound
directly to the reference. It does not travel through a function return, a
member initializer, or a pointer:
const std::string& name = std::string("Ada"); // extended
// const std::string& bad = make_string(); // not extended
For a deeper treatment, see the dangling string_view entry and the Lifetime lab.
5. API design: choose the constraint you mean¶
| Signature | Meaning | Use when |
|---|---|---|
T& |
must name a mutable object | in/out parameter, or a mutating operation |
const T& |
must name a readable object; may bind a temporary | read-only input, especially large objects |
T* |
may be null, may be reseated | optional access; document ownership separately |
std::unique_ptr<T> |
owns exactly one object | ownership transfer |
std::shared_ptr<T> |
shared ownership | lifetime is genuinely shared |
std::reference_wrapper<T> |
a copyable, reseatable reference | containers or callbacks that need reference semantics |
The reference-versus-pointer table from the old version of this page is useful, but it misses the important point: the choice is about contract, not syntax.
When a reference must be stored¶
A reference member is legal, but it makes the containing type non-assignable
unless you provide a custom assignment operator. If you need reference
semantics in a container, use std::reference_wrapper<int>:
std::vector<std::reference_wrapper<int>> values;
int a = 1;
values.push_back(a); // stores a reference, not a copy
reference_wrapper is copyable and assignable; it still does not extend the
lifetime of a. The owner must outlive the container.
6. Common mistakes¶
- Returning a reference to a local — dangling as soon as the function returns.
- Binding a reference to a member of a temporary — the temporary dies at the end of the full expression; the member reference does not save it.
- Assuming
const T&makes an object immutable — it only limits what this reference can do; another non-const alias may exist. - Using
T&for an optional parameter — a reference cannot express "no object"; useT*orstd::optional<std::reference_wrapper<T>>. - Confusing
T&&in a template with an rvalue reference — deduction can make it a forwarding reference. - Using a reference after the container reallocates — the reference points to the old storage, not to the moved element.
7. Review checklist¶
- Is the referenced object guaranteed to outlive every use of the reference?
- Does the API need nullability or rebinding? If yes, use a pointer or a
reference_wrapper, not a reference. - Is the reference
constbecause the operation is read-only, or because the object itself is const? - Is a temporary being bound directly to the reference, or is it hidden behind a function return or a member access?
- Does a container operation invalidate the referenced element?
Runnable example¶
The complete program for this topic is:
It is compiled and run by the repository's example checks. The smaller snippets on this page are illustrative fragments, not standalone programs.
Standard references¶
[dcl.ref]— reference declarators[dcl.init.ref]— reference initialization and binding[expr.ref]— class member access[class.temporary]— temporary objects and lifetime extension- cppreference: reference declaration
- cppreference: reference initialization