Skip to content
Latchkey

gcc/clang "cannot bind non-const lvalue reference to an rvalue" in CI

A non-const lvalue reference can only bind to a modifiable, named object. A temporary (rvalue) cannot bind to it because modifications would be lost when the temporary dies.

What this error means

Passing a literal, a function result, or a constructed temporary to a parameter of type T& fails with "cannot bind non-const lvalue reference of type T& to an rvalue".

gcc
app.cpp:9:7: error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
    9 |   inc(5);
      |       ^

Common causes

How to fix it

Pass an lvalue or change the parameter

  1. Pass a named variable so a modifiable lvalue exists.
  2. If the function does not modify the argument, take const T& or T by value.
gcc
int x = 5; inc(x);          // bind to a named lvalue
void inc(const int& n);     // or take const ref if read-only

How to prevent it

  • Take const T& or by-value parameters for read-only inputs, and pass named lvalues when a function must modify its argument.

Frequently asked questions

What causes ""cannot bind non-const lvalue reference""?
Passing a literal, a function result, or a constructed temporary to a parameter of type T& fails with "cannot bind non-const lvalue reference of type T& to an rvalue".
How do I fix "cannot bind non-const lvalue reference"?
Pass an lvalue or change the parameter

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card