C++ Reference ยท Types and Language Concepts
References (lvalue references)
Reference for C++ lvalue references: binding rules, const references and temporaries, reference parameters and members, reference_wrapper and dangling references.
An lvalue reference is an alias: another name for an existing object. You declare one with & after the type, int& r = x;, and from then on r and x are the same object. A reference must be initialized when it is created, can never be reseated to refer to something else, and (unlike a pointer) is never null in valid code.
References are how C++ passes large objects without copying (const std::string&), lets a function modify its argument (void inc(int&)), makes operator[] assignable, and lets a range-based for loop change container elements. A const lvalue reference can additionally bind to a temporary, extending its lifetime to that of the reference.
int x = 1; int& r = x; // r is another name for x r = 5; // x is now 5 const int& c = 42; // binds to a temporary; its lifetime is extended
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
T& name = lvalue; // lvalue reference const T& name = anything; // const lvalue reference: binds to lvalues and temporaries T&& name = rvalue; // rvalue reference (see the next page)
| Form | Binds to |
|---|---|
T& |
Modifiable lvalues of type T (or a derived type) only. |
const T& |
Lvalues, const lvalues and rvalues (temporaries). Lifetime of a temporary is extended. |
volatile T& |
Only volatile or non-volatile lvalues; a rare combination. |
T& as a parameter |
Argument is passed without copying; the function can change it. |
T& as a return type |
The result is an lvalue (usable on the left of =). Must refer to something that outlives the function. |
Errors / undefined behavior: using a reference after the object it refers to is destroyed (a dangling reference) is undefined behavior. Returning a reference to a local variable or binding a reference to the result of a function that returns a temporary member of a temporary are classic causes. Initializing a reference from a null pointer dereference is undefined behavior.
Usage notes
- A reference is not an object. It has no separate identity you can address;
&ris the address of the referenced object. The compiler usually implements it as a pointer, but the language does not promise storage. - Initialization is binding, assignment is copying.
r = y;assigns to the referenced object; it never makesrrefer toy. If you need something reseatable, use a pointer orstd::reference_wrapper. - Pass by
const T&for large read-only objects; pass small trivially copyable types (int,double, pointers,std::string_view,std::span) by value. - Lifetime extension has limits. It applies to a temporary bound directly to a local reference, not to a temporary returned through a function that returns a reference, and not when the reference is a class member initialized in a constructor's member-initializer list (the temporary dies at the end of the constructor).
- Reference members make a class non-assignable by default (the copy assignment operator is deleted). Prefer pointers or
std::reference_wrapperwhen assignment is needed. - No arrays of references and no pointers to references. References to arrays and references to functions are fine.
autodrops references:auto v = r;makes a copy; writeauto& v = r;to keep an alias. In range-for,for (auto x : c)copies each element.- Range-for temporary pitfall. Before C++23,
for (auto& e : f().member())could dangle; C++23 extends lifetimes of temporaries in the range-initializer. - Reference collapsing, the rule behind
T&&in templates, is covered under Forwarding references.