Skip to content

[clang++] Can not capture member of structured binding to lambda in constant evaluation. #134897

Open
@MiroPalmu

Description

@MiroPalmu

Clang rejects following code while gcc compiles it.

https://godbolt.org/z/E3TcTq1hx

struct bar {
    int x;
};

consteval int foo() {
    const auto [a] = bar{};

    return [=] {
        return a;
    }();
}

int main() {
    return foo();
}

Error message from clang:

<source>:14:12: error: call to consteval function 'foo' is not a constant expression
   14 |     return foo();
      |            ^
<source>:9:16: note: read of non-constexpr variable '[a]' is not allowed in a constant expression
    9 |         return a;
      |                ^
<source>:8:12: note: in call to '[=] {
    return a;
}.operator()()'
    8 |     return [=] {
      |            ^~~~~
    9 |         return a;
      |         ~~~~~~~~~
   10 |     }();
      |     ~~~
<source>:14:12: note: in call to 'foo()'
   14 |     return foo();
      |            ^~~~~
<source>:6:16: note: declared here
    6 |     const auto [a] = bar{};
      |                ^
1 error generated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constevalC++20 constevallambdaC++11 lambda expressions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions