Skip to content

Errors capturing structured bindings in generic lambda with arguments #58420

Closed as not planned
@jdrouhard

Description

@jdrouhard
struct Foo {
    int first;
    int second;
};

void bar() {
    Foo f;
    auto &[first, second] = f;
    auto works1 = [&first]() { (void) first; };
    auto works2 = [&]() { (void) first; };
    auto bug1 = [&first](auto arg) { (void) first; }; // -Wunused-lambda-capture
    auto bug2 = [&](auto arg) { (void) first; }; // error: reference to local binding 'first' declared in enclosing function 'bar'
    works1();
    works2();
    bug1(42);
    bug2(42);
}

https://godbolt.org/z/zzqdf9sa8

When a generic lambda attempts to capture a structured binding (legal since c++20), clang either emits an unused-lambda-capture warning for explicit structured binding captures, or it fails to compile if the structured binding is used in a generic lambda with an implicit default capture.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"duplicateResolved as duplicate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions