Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Lvalue reference (`R&`)

`R&` aliases an existing object. Assigning through it modifies that object, and returning `R&` lets the caller keep referring to the same object without making a copy.

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

Syntax

R& ref = target;
R& get();
const R& read_only = target;

template<class R, class F>
R& run_interval(R& result, F&& namedf, int first, int last);

An lvalue reference (R&) aliases an existing object. In R& ref = target;, initialization binds ref to target without copying it. Here & is part of the declarator, not the address-of operator. A later ref = value; assigns to the object if that assignment is valid; it does not rebind the reference.

An int& can bind to an ordinary int variable, but not to a const int or the literal 42. A const int& does not allow modification through that reference. It can also bind to a temporary; whether that extends the temporary's lifetime depends on the binding context.

In the example, R& result aliases the caller's variable. For each i in [first, last), result = namedf(i); writes to that same variable. The function returns R&, so the call is an lvalue and auto& alias binds to the same object. Returning by value does not provide that alias.

F&& is a forwarding reference here because F is a deduced type parameter of this function template with no const or volatile qualifier. The expression namedf itself is an lvalue, even if the deduced parameter type is an rvalue reference. Thus namedf(i) calls the callable as an lvalue. Using std::forward<F>(namedf) would preserve the lvalue-versus-rvalue distinction, not necessarily the exact value category: the lambda argument is a prvalue at the call site, but std::forward<F>(namedf) would produce an xvalue. Calling through that expression could select a different operator() overload.

[&] captures the local variable base by reference because the lambda uses it. The global offset is accessible without a capture. Neither returning a reference nor capturing by reference extends the lifetime of these local variables. Do not return a reference to a local variable that dies when the function exits: reading through the resulting dangling reference is undefined behavior.

Examples

#include <iostream>

int offset = 1;

template<class R, class F>
R& run_interval(R& result, F&& namedf, int first, int last)
{
    for (int i = first; i < last; ++i) {
        result = namedf(i);
    }
    return result;
}

int main()
{
    int result = 0;
    int base = 39;
    auto& alias = run_interval(result, [&](int i) {
        return base + offset + i;
    }, 0, 3);

    std::cout << result << '\n';
    alias = 43;
    std::cout << result << '\n';
}

Result

42
43

For `i` equal to `0`, `1`, and `2`, the loop writes `40`, `41`, and `42`. Then `alias = 43` modifies the same `result` variable, so the second line is `43`.

Related lessons

  • C++ · Lesson 24 · Advanced · Version: 3

Sources