Bug 85889 - lambda expression can capture structured bindings
Summary: lambda expression can capture structured bindings
Status: SUSPENDED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.1.1
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2018-05-23 14:07 UTC by Jonathan Wakely
Modified: 2018-11-10 16:19 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-05-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2018-05-23 14:07:04 UTC
[expr.prim.lambda.capture] p8 says "If a lambda-expression explicitly captures an entity that is not odr-usable or captures a structured binding (explicitly or implicitly), the program is ill-formed."

With -std=c++17 this compiles without any diagnostic:

struct X { int i, j; };
void f() {
  X x{};
  auto [i, j] = x;
  [&i]() { };
}


Clang says:

l.cc:5:5: error: 'i' in capture list does not name a variable
  [&i]() { };
    ^
l.cc:5:3: warning: expression result unused [-Wunused-value]
  [&i]() { };
  ^~~~~~~~~~
1 warning and 1 error generated.


And EDG says:

"l.cc", line 5: error: structured binding cannot be captured
    [&i]() { };
      ^

"l.cc", line 4: warning: variable "i" was declared but never referenced
    auto [i, j] = x;
          ^

"l.cc", line 4: warning: variable "j" was declared but never referenced
    auto [i, j] = x;
             ^

1 error detected in the compilation of "l.cc".
Comment 1 Marek Polacek 2018-05-29 18:24:31 UTC
Confirmed.
Comment 2 Ville Voutilainen 2018-05-29 22:14:35 UTC
Please suspend this bug. The change that made capturing bindings ill-formed in P0588R1 was not in the design paper P0588R0. I plan to propose to revert this change, and make capturing structured bindings well-defined in all standard versions that support structured bindings.
Comment 3 Jonathan Wakely 2018-05-29 22:19:44 UTC
Done.