Skip to content

Wrong error message when mutate const object capturing by reference #98772

@vovchikzd

Description

@vovchikzd

When trying to create a mutable lambda with const object captured by reference and then mutate that object

int main() {
    const int a = 69;
    auto lambda = [&r = a](int x) mutable {
        r += x;
        return r;
    };
}

you got incorrect error message

<source>:4:11: error: cannot assign to a variable captured by copy in a non-mutable lambda
    4 |         r += x;
      |         ~ ^

but r is captured by reference and lambda is mutable
godbolt example: https://godbolt.org/z/sKd3bh6aM

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerlambdaC++11 lambda expressions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions