Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Lvalue reference declarator (&)

A declarator’s `&` forms an lvalue reference type. Learn what it can bind to, when you can write through it, and how to avoid dangling references.

Version: ISO C++23; implementation support may vary

Syntax

int x = 2;
int& r = x;
const int& view = x;
const int& temporary = 42;
void update(int& value);
int observe(const int& value);
int& bump(int& value);
using IntRef = int&;
int a[3]{};
int (&row)[3] = a;

In int& r = x;, the lvalue reference declarator (&) makes r an alias for x; binding does not copy x. A local reference definition needs an initializer, but a block-scope reference declaration explicitly marked extern can omit it. Once bound, it cannot be reseated: r = y; assigns to x instead of binding r to y.

int& binds to a non-const int lvalue, but not to the literal 42. const int& can bind to a const or non-const int lvalue or to the temporary materialized from 42. const prevents writes through that reference, not changes through another alias. If the only overloads are f(int&) and f(const int&), a non-const int lvalue selects the former, and f(42) selects the latter.

A T& parameter lets you modify the caller’s object if T allows it. A T& return lets the caller access an existing object. When the iterator yields an element as an lvalue, for (const auto& item : items) binds to the element without copying it. int (&row)[3] is a reference to the whole three-element int array.

A reference does not own its referent. The local declaration const int& t = 42; extends the temporary’s lifetime to match t’s. In an ordinary call, a temporary bound to a reference parameter lasts only to the end of the full-expression containing the call. Returning that reference does not extend the temporary’s lifetime. Do not return a reference to an automatic local object: reading through a dangling reference is undefined behavior.

In an expression, built-in &x takes an address, while 6 & 3 is bitwise AND. int&& denotes an rvalue reference, [&] is a lambda’s capture-by-reference default, and the trailing & in void f() &; is a member-function ref-qualifier that requires an lvalue object.

Examples

#include <iostream>

int& bump(int& value) {
    ++value;
    return value;
}

int observe(const int& value) {
    return value;
}

int main() {
    int value = 2;
    int other = 7;
    auto& ref = bump(value);
    std::cout << "after bump: " << value << '\n';

    ref = other;
    ++other;
    int* ptr = &ref;
    std::cout << "value: " << observe(ref) << ", other: " << other << '\n';
    std::cout << "same object: " << static_cast<int>(ptr == &value)
              << ", mask: " << (6 & 3)
              << ", temporary: " << observe(42) << '\n';
}

Result

after bump: 3
value: 7, other: 8
same object: 1, mask: 2, temporary: 42

`bump` changes `value` from 2 to 3 and returns a reference to that same object. `ref = other` stores 7 in `value`; incrementing `other` to 8 leaves `value` unchanged. The address comparison confirms the object’s identity. `6 & 3` yields 2, and `observe(42)` safely reads a temporary during the call.

Related lessons

  • C++ · Lesson 1 · Advanced · Version: 3
  • C++ · Lesson 2 · Advanced · Version: 3
  • C++ · Lesson 3 · Advanced · Version: 3
  • C++ · Lesson 5 · Advanced · Version: 3
  • C++ · Lesson 6 · Advanced · Version: 3
  • C++ · Lesson 9 · Advanced · Version: 3
  • C++ · Lesson 10 · Advanced · Version: 3
  • C++ · Lesson 11 · Advanced · Version: 3
  • C++ · Lesson 12 · Advanced · Version: 3
  • C++ · Lesson 13 · Advanced · Version: 3
  • C++ · Lesson 14 · Advanced · Version: 3
  • C++ · Lesson 15 · Advanced · Version: 3
  • C++ · Lesson 16 · Advanced · Version: 3
  • C++ · Lesson 18 · Advanced · Version: 3
  • C++ · Lesson 19 · Advanced · Version: 3
  • C++ · Lesson 21 · Advanced · Version: 3
  • C++ · Lesson 22 · Advanced · Version: 3
  • C++ · Lesson 23 · Advanced · Version: 3
  • C++ · Lesson 25 · Advanced · Version: 3
  • C++ · Lesson 26 · Advanced · Version: 3
  • C++ · Lesson 27 · Advanced · Version: 3
  • C++ · Lesson 28 · Advanced · Version: 3
  • C++ · Lesson 29 · Advanced · Version: 3
  • C++ · Lesson 30 · Advanced · Version: 3
  • C++ · Lesson 31 · Advanced · Version: 3
  • C++ · Lesson 32 · Advanced · Version: 3
  • C++ · Lesson 33 · Advanced · Version: 3
  • C++ · Lesson 34 · Advanced · Version: 3
  • C++ · Lesson 35 · Advanced · Version: 3
  • C++ · Lesson 36 · Advanced · Version: 3
  • C++ · Lesson 38 · 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
  • C++ · Lesson 48 · Advanced · Version: 3
  • C++ · Lesson 49 · Advanced · Version: 3
  • C++ · Lesson 51 · Advanced · Version: 3
  • C++ · Lesson 53 · Advanced · Version: 3
  • C++ · Lesson 54 · Advanced · Version: 3
  • C++ · Lesson 55 · Advanced · Version: 3
  • C++ · Lesson 57 · Advanced · Version: 3
  • C++ · Lesson 59 · Advanced · Version: 3
  • C++ · Lesson 60 · Advanced · Version: 3

Sources