Capture of *this in explicit object parameter lambda doesn't respect qualifiers (original) (raw)

struct S { int x;

auto foo() { return [*this](this auto&&) { __builtin_printf("%d ", x); x = 10; }; } };

int main() { S s{ 5 }; const auto l = s.foo(); l(); s.x = 15; l(); __builtin_printf("%d\n", s.x); }

https://godbolt.org/z/35qjr473K

Expected: an error that you cannot modify x within a const lambda.

Result: Prints 5 10 15, demonstrating both that l has captured a copy of S, and also that (despite being a const object) it is nevertheless mutating its own state.