Skip to content

Capture of *this in explicit object parameter lambda doesn't respect qualifiers #84163

Closed
@wreien

Description

@wreien
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.

Metadata

Metadata

Assignees

Labels

c++23clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second partylambdaC++11 lambda expressions

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions