C++

Fix 'Cannot Bind Non-const Lvalue Reference to an Rvalue' in C++ — Temporaries, Function Arguments, and const&

A non-const lvalue reference promises its caller a named object it can change. A temporary makes no such promise: it is a value on its way out, and letting a function silently mutate it would change a copy nobody can read back. This guide reads the GCC diagnostic word by word, shows which bindings a temporary will accept, and fixes the failing calls by stating the real intent in the parameter type.

Written November 21, 2026 · Tested with GCC 13.3.0 · 16 min read

By the end of this guide
  • The first failure: binding a reference directly to a literal
  • The second failure: the same binding inside a function call
  • The contrast: the two bindings a temporary will accept
  • The rule underneath both errors

Written November 21, 2026

Tested with GCC 13.3.0

What this guide is

Every expression in C++ has a value category as well as a type. A literal such as 42 and a computed result such as x + 1 are prvalues: they compute a value but do not name a lasting object. The name of a variable is an lvalue: it identifies an object whose address can be taken and whose value persists past the expression that mentions it. An rvalue is a prvalue or an xvalue, and a non-const lvalue reference — written int&, without const and with a single & — can be initialized only from an lvalue. Offer it an rvalue and the program is ill-formed, with this error as the report.

That restriction is the feature, not an obstacle in front of it. A parameter of type int& advertises that the function may write through the reference, and the caller expects to read the change back through the original object afterward. A write into a temporary would satisfy neither side: the temporary is destroyed at the end of the full expression containing the call, so the changed value would vanish before anyone could observe it, and the call site gives no sign that anything was supposed to change. The compiler therefore rejects the binding at the point it is attempted, in a variable declaration and in a function argument alike — function arguments are initialized from the caller's expressions by the same reference-initialization rules as variables, so one rule produces both failures in this guide.

This guide works through the two failing forms first, in the order they usually arrive: a direct binding, int& r = 42;, and then the same binding hidden inside a function call. It then shows the contrast program that does compile, where a const int& and an int&& each bind to the same literal, and finishes with a fixed program that keeps all three intents — reading, mutating, and taking ownership — each expressed with the reference type that matches it.

Build every program in this guide with g++ -std=c++20 -g. Nothing here needs C++20 specifically; value categories, rvalue references, and std::move behave the same way in the earlier standards that have them.

The first failure: binding a reference directly to a literal

The smallest failing program is three lines long:

C++
int main() {
    int& r = 42;
    return r;
}
Terminal
$ g++ -std=c++20 -g -o bind bind.cpp
bind.cpp: In function ‘int main()’:
bind.cpp:2:14: error: cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’
    2 |     int& r = 42;
      |              ^~

Read the error in its three named parts, because every variant of this diagnostic uses the same three. The reference being initialized is a non-const lvalue reference of type 'int&': the non-const is load-bearing — adding const changes the outcome, as the contrast program below shows. The thing offered to it is an rvalue of type 'int': the literal 42 is a prvalue, the purest form of rvalue, an expression that computes a value and identifies no object. And the verb is cannot bind: reference initialization is a binding, not a copy, so there is no fallback in which the compiler quietly makes a variable to hold the 42 and binds r to that instead. The caret span sits under 42, the initializer, not under r — the declaration of the reference is fine in itself; the initializer is what fails it.

The rule behind the wording is exact: if the reference to be initialized is an lvalue reference and the initializer is not an lvalue of a compatible type, the program is ill-formed when the reference is not const-qualified. There is one narrow exception in the reference-initialization rules, for a class-type initializer that can be converted to an lvalue through a conversion function — int has no conversion functions, so no exception is anywhere near this program. For the common scalar and library types, the practical reading is the simple one: a non-const & needs a named object on the right-hand side.

Note what the error is not. It is not a complaint that 42 has the wrong type — the type named on both sides is int. Changing the literal to any other rvalue of type int, a function call returning by value or the result of an arithmetic expression, produces the same error with the same wording, because the failing property is the value category, not the type. That is also why searching on the type in the message so often misleads: two programs can agree on every type in sight and still differ on this error, purely because one side named an object and the other computed a value.

The second failure: the same binding inside a function call

In real code the binding rarely appears as a declaration. It appears as a function argument, where the parameter is the reference being initialized and the argument is the initializer:

C++
void increment(int& value) {
    ++value;
}

int main() {
    int x = 41;
    increment(42);
    increment(x + 1);
    return x;
}
Terminal
$ g++ -std=c++20 -g -o call call.cpp
call.cpp: In function ‘int main()’:
call.cpp:7:15: error: cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’
    7 |     increment(42);
      |               ^~
call.cpp:1:21: note:   initializing argument 1 of ‘void increment(int&)’
    1 | void increment(int& value) {
      |                ~~~~~^~~~~
call.cpp:8:17: error: cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’
    8 |     increment(x + 1);
      |               ~~^~~
call.cpp:1:21: note:   initializing argument 1 of ‘void increment(int&)’
    1 | void increment(int& value) {
      |                ~~~~~^~~~~

The output has two errors, and both are the first failure wearing a call site. Each error: line names the same reference type, int&, and the same offered category, an rvalue of type int. What the call form adds is the note: line after each error, initializing argument 1 of 'void increment(int&)', with a caret span under the parameter declaration on line 1. Read the pair together: the error line gives the call site and the argument that failed, the note gives the parameter that the argument was trying to initialize. In a long build log that note is the line that tells you which function's signature is involved, and it is worth looking for before changing anything, because the fix may belong in the signature rather than at any one call.

The two arguments fail for the same reason, and seeing why the second one fails is the step that makes the rule stick. 42 is a literal, plainly a prvalue. x + 1 mentions a variable, but the expression itself is a built-in arithmetic expression, and built-in arithmetic expressions are prvalues: x + 1 computes a new value, 42, in no lasting object. The x inside it is an lvalue; the sum is not. So passing an expression that merely contains a variable is not passing the variable. increment(x) would bind without complaint, because the argument expression is then the name of a variable, an lvalue, and the write inside increment would land in x, where the caller can read it back — which is precisely the contract the parameter type promised.

There is a temptation at this point that does not work, and it is worth ruling out explicitly: wrapping the argument in std::move does not turn it into something an int& will accept. std::move produces an xvalue, and an xvalue is still an rvalue, so increment(std::move(x)) fails with this same error. std::move changes which reference an argument can bind to in the other direction — it makes a named object acceptable where an rvalue reference is required, as the fixed program below uses it — it never makes a temporary, or a cast-to-rvalue variable, acceptable where a non-const lvalue reference is required.

The contrast: the two bindings a temporary will accept

The same literal that int& refused binds without complaint to either of the other two reference forms:

C++
#include <iostream>

int main() {
    const int& cr = 42;
    int&& rr = 42;
    std::cout << "const ref = " << cr << '\n';
    std::cout << "rvalue ref = " << rr << '\n';
    rr = 43;
    std::cout << "rvalue ref after assign = " << rr << '\n';
    return 0;
}
Terminal
$ g++ -std=c++20 -g -o contrast contrast.cpp
$ ./contrast
const ref = 42
rvalue ref = 42
rvalue ref after assign = 43

The const int& line is the everyday form. A const lvalue reference may bind to an rvalue: the prvalue is materialized into a temporary object, the reference binds to that object, and — this is the part that makes the idiom useful rather than merely legal — when a reference is bound to a temporary, the lifetime of the temporary is extended to match the lifetime of the reference. So cr does not dangle at the end of its own declaration statement; the temporary holding 42 lives as long as cr does. The price of the binding is exactly the const: the reference is read-only, so the write-through-the-reference scenario the non-const form was rejected for cannot arise. That trade is why const T& is the standard parameter type for a function that only reads a large argument: it accepts named objects and temporaries alike, copies neither, and promises not to change either.

The int&& line is the other form. An rvalue reference binds to rvalues by design — to prvalues directly, and to xvalues such as the result of std::move. It is not const: the assignment rr = 43; in the program is legal, and the third output line shows the new value read back through the same reference. Two cautions keep this form in its place. First, the name of an rvalue reference variable is itself an lvalue expression — in the lines after its declaration, rr names a lasting object, which is why it can be assigned to and read from like any variable, and why passing it on to another function that wants an rvalue reference takes another std::move. Second, lifetime extension applies here just as it does for the const reference: the temporary materialized for rr lives as long as rr does. What an rvalue reference parameter in a function signature adds on top of this variable form is a statement of intent — the caller is handing over a value the function may treat as disposable, typically because the function will move resources out of it.

One exception to the lifetime-extension rule matters for function parameters specifically, and it is the boundary of the const-reference idiom: a temporary bound to a reference parameter in a function call exists only until the end of the full expression containing the call. Inside the call, a const int& parameter bound to 42 is perfectly safe to read. If the function stores that reference somewhere that outlives the call, or returns it, the stored or returned reference dangles — the extension that protects a local const int& variable does not follow a parameter's temporary past the call. The binding is legal in both places; only the lifetime differs, and the difference is in the declaration site, not in the parameter type.

The rule underneath both errors

A non-const lvalue reference requires an lvalue initializer; a const lvalue reference and an rvalue reference each accept an rvalue, by different bargains — the const reference gives up the right to modify, and the rvalue reference declares that the value is disposable. Every instance of this error is therefore a mismatch between intent and spelling at one binding site: either the code offered a value where the signature demanded an object, or the signature demanded an object where the code only ever had a value.

So the diagnostic always contains the two coordinates needed to fix it — the binding site in the error: line, and, for the call form, the parameter in the note: line. The fix is always a decision about intent, made once per signature rather than once per call site: if the function only reads, the parameter is misspelled and should be const&; if the function must write back, the parameter is right and the call site is wrong, and the argument must become a named variable whose new value the caller then uses; if the function takes ownership of a value, the parameter should be && (or taken by value), and callers with named objects opt in with std::move. No instance of this error is fixed by a cast that fakes an lvalue out of a temporary: a direct cast of a prvalue to an lvalue reference type — static_cast<int&>(42), a C-style cast, or a reinterpret_cast — is itself rejected as an invalid cast, because the rule that a cast to an lvalue reference type is an lvalue expression applies to casts whose operand is already a glvalue, not to prvalues. The cast route that does compile, laundering a temporary through a const& and a const_cast, buys only a reference whose safety rests entirely on that const reference's lifetime — it states no intent and fixes nothing.

The fixed program: one parameter type per intent

The fixed program keeps the failing shapes and gives each one the spelling its intent requires. Reading a value uses const int&, which accepts the literal that failed before. Mutating uses int&, called with a named variable whose changed value is then printed. Taking ownership uses a std::string&& parameter, called once with a named string the caller hands over through std::move and once with a temporary string directly:

C++
#include <iostream>
#include <string>
#include <utility>

// Read-only: const& accepts lvalues and temporaries alike.
void show(const int& value) {
    std::cout << "show = " << value << '\n';
}

// Mutation intended: int& requires a named lvalue at the call site.
void increment(int& value) {
    ++value;
}

// Ownership taken: an rvalue reference parameter accepts a temporary
// directly, and a named object only through std::move.
void consume(std::string&& text) {
    std::cout << "consumed = " << text << '\n';
}

int main() {
    show(42);

    int n = 41;
    increment(n);
    std::cout << "incremented = " << n << '\n';

    std::string name = "veldica";
    consume(std::move(name));
    consume(std::string("temporary"));
    return 0;
}
Terminal
$ g++ -std=c++20 -g -o fixed fixed.cpp
$ ./fixed
show = 42
incremented = 42
consumed = veldica
consumed = temporary

Read the four output lines as the three intents discharged. show = 42 is the read-only call: the literal that no non-const reference would take is an ordinary argument to a const int& parameter, valid for the duration of the call, which is all a reading function needs. incremented = 42 is the mutation call done honestly: the increment lands in n, a named object, and the next line reads the result back through that name — the round trip that a temporary argument could never have completed. The two consumed lines are the ownership calls, and they show the two ways an rvalue reference parameter gets its argument: std::move(name) converts the named string into an xvalue, which is exactly what std::move is — an indication that the object may be moved from, equivalent to a cast to an rvalue reference — while the temporary std::string("temporary") is already a prvalue and needs no conversion at all. Without the std::move, the first of those calls fails in the mirror-image direction, with an error about binding an rvalue reference to an lvalue: the two reference forms are strict in opposite directions, and std::move is the explicit crossing from the named side to the disposable side. Nothing in the fixed program prints the state of name after the move, deliberately: a moved-from standard library object is in a valid but unspecified state, so code after a move may assign to it or destroy it, but may not assume a particular value for it.

The fixes, ranked

Change the parameter to const T& when the function only reads. Ranked first because it is the fix the majority of real occurrences need: the signature over-promised, the call sites were innocent, and one edit to the declaration repairs every failing call at once while also accepting every argument the old signature accepted. Check the function body for writes through the parameter before applying it — a body that writes is telling you the intent was mutation, and this fix would then only move the error inside the function.

Pass a named variable when the caller needs the changed value. Declare a variable, initialize it with the value the temporary would have carried, pass the variable, and use the variable after the call. This is the only fix that preserves the write-back semantics of an int& parameter, and it makes the data flow visible at the call site, where the original code hid it inside an expression. For increment(x + 1), the honest form is a variable holding x + 1, incremented, and then assigned or used explicitly.

Change the parameter to T&&, or take the argument by value, when the function takes ownership. An rvalue reference parameter states that the function may move from its argument; temporaries then bind directly, and callers with named objects cross over explicitly with std::move. Taking the parameter by value and moving from the parameter inside the function achieves the same ownership transfer with one overload instead of two, at the cost of a move construction the rvalue-reference form avoids spelling out.

Add an overload instead of replacing the signature, when both intents are real. A const T& overload for reading callers alongside the T& overload for mutating callers, or an &/&& overload pair, lets each call site pick by argument category. Reach for this last: overloads chosen by value category are easy to mispredict at a call site, and a single signature that states one intent is easier to audit than a pair whose selection depends on the argument's category.

Which fix for which cause

What the diagnostic shows Cause Fix
Error at T& name = literal; A temporary offered to a non-const lvalue reference variable Bind a const T& instead, or name the value in a variable first
Error at a call, argument is a literal Function reads, or caller expected write-back into nothing const T& parameter if it reads; a named variable if write-back is real
Error at a call, argument is an arithmetic or function-call result The expression is a prvalue even though it mentions variables Store the expression in a named variable and pass that
Same error after adding std::move to the argument std::move yields an xvalue, still an rvalue std::move never satisfies a T& parameter; use a named variable
Error names an rvalue reference parameter and an lvalue argument Named object offered where a disposable value is required Pass std::move(name) if the object is truly disposable
Error inside a function after changing its parameter to const& The body writes through the parameter The intent was mutation: restore T& and fix the call sites

Checklist

  • Read the error's three parts before editing: the reference type named first is the spelling to question, and the category named second — rvalue — is the property of the argument that no type change to the argument will remove.
  • For the call form, read the note: line: it names the parameter being initialized, which is where a signature fix belongs, and one signature fix repairs all of that function's failing calls together.
  • Classify the argument expression, not its ingredients: a literal, an arithmetic result, and a by-value function result are prvalues even when a variable appears inside them; only a named object, or an expression the rules list as an lvalue, binds to a non-const &.
  • Decide the intent before touching the spelling: read-only means const&, write-back means & plus a named variable at the call site, ownership means && or by-value plus std::move at the call sites that hand objects over.
  • Do not reach for std::move as a universal solvent: it converts a named object into an rvalue for && parameters, and does nothing at all for a & parameter except restate the same error.
  • Do not try to cast a temporary to an lvalue reference to silence the diagnostic: a direct cast of a prvalue to an lvalue reference type is itself an invalid cast, and laundering the temporary through a const& with a const_cast states no intent — fix the parameter type or the call site instead.
  • If a const& parameter's reference is stored or returned, check its lifetime separately from its legality: a temporary bound to a parameter lives only to the end of the full expression containing the call, unlike a temporary bound to a local const& variable, whose lifetime is extended to the reference's lifetime.

Sources

  • cppreference, Reference initialization — for a non-const lvalue reference being ill-formed when its initializer is not an lvalue, for function arguments being initialized by the same rules, for the lifetime of a temporary bound to a reference being extended to match the reference, and for the exception that a temporary bound to a reference parameter lasts only to the end of the full expression containing the call.
  • cppreference, Value categories — for literals and built-in arithmetic expressions being prvalues, for the name of a variable being an lvalue even when the variable's type is an rvalue reference, for an rvalue being a prvalue or an xvalue, and for a cast to an lvalue reference type being an lvalue expression.
  • cppreference, std::move — for std::move indicating that an object may be moved from, for it producing an xvalue exactly equivalent to a static_cast to an rvalue reference type, for rvalue reference overloads being selected by prvalue and xvalue arguments, and for moved-from standard library objects being in a valid but unspecified state.
  • cppreference, Lifetime — for temporary objects being destroyed as the last step in evaluating the full expression that contains the point where they were created, and for lifetime extension by binding to a reference being the exception to that rule.

Keep Exploring