https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118719

            Bug ID: 118719
           Summary: [14/15 regression] Spurious -Wc++20-extensions warning
                    emitted when capturing a structured binding
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dangelog at gmail dot com
  Target Milestone: ---

Hi,

This is a spin-off of bug 85889, comment 8 .

GCC 15 added a compatibility warning when capturing a structured binding in
lambda's capture list, in pre-C++20 modes. The problem is that the warning is
also emitted when a structured binding is used in an init-capture's
initializer, not just in a simple-capture:

https://gcc.godbolt.org/z/TnzWs8xne

int main()
{
    int a[] = { 42 };

    auto [x] = a;

    [x](){}; // 1
    [x=x](){}; // 2
    [test = x](){}; // 3
    [test = x * 2](){}; // 4
}

> <source>: In function 'int main()':
> <source>:7:6: warning: captured structured bindings are a C++20 extension 
> [-Wc++20-extensions]
>     7 |     [x](){}; // 1
>       |      ^
> <source>:5:11: note: declared here
>     5 |     auto [x] = a;
>       |           ^
> <source>:8:8: warning: captured structured bindings are a C++20 extension 
> [-Wc++20-extensions]
>     8 |     [x=x](){}; // 2
>       |        ^
> <source>:5:11: note: declared here
>     5 |     auto [x] = a;
>       |           ^
> <source>:9:13: warning: captured structured bindings are a C++20 extension 
> [-Wc++20-extensions]
>     9 |     [test = x](){}; // 3
>       |             ^
> <source>:5:11: note: declared here
>     5 |     auto [x] = a;
>       |           ^
> Compiler returned: 0


(1) is OK, but (2) and (3) shouldn't warn. (4) doesn't warn.

AFAICS, the code should be fine, as init-captures don't impose anything here?

* In C++17 (N4659), [expr.prim.lambda.capture]/6: "An init-capture behaves as
if it declares and explicitly captures a variable of the form “auto
init-capture ;” whose declarative region is the lambda-expression's
compound-statement"
(https://timsong-cpp.github.io/cppwp/n4659/expr.prim.lambda.capture#6)

* The current draft says: "An init-capture without ellipsis behaves as if it
declares and explicitly captures a variable of the form “auto init-capture ;”"
(https://eel.is/c++draft/expr.prim.lambda.capture#6)

For comparison, Clang doesn't warn on (2) and (3).

Reply via email to