https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122163
Bug ID: 122163
Summary: Lambda use of rvalue ref not considered an lvalue
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: nshead at gcc dot gnu.org
Target Milestone: ---
Consider:
int x;
int main() {
constexpr int&& r = static_cast<int&&>(x);
r = 1;
// not an ODR-use of 'r' so no capture needed.
auto a = [] { r = 2; };
auto b = [&] { r = 3; };
a();
if (r != 2)
__builtin_abort();
b();
if (r != 3)
__builtin_abort();
}
GCC errors, saying
<source>: In lambda function:
<source>:7:21: error: using rvalue as lvalue [-fpermissive]
7 | auto a = [] { r = 2; };
| ^
<source>: In lambda function:
<source>:8:22: error: using rvalue as lvalue [-fpermissive]
8 | auto b = [&] { r = 3; };
| ^
This goes back to at least GCC 8; GCC 7 incorrectly complained that 'r' was not
captured for 'a' but is OK with 'b'. Clang compiles this code correctly.