-
Notifications
You must be signed in to change notification settings - Fork 11.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Clang] Incorrect error on capture list + lambda. #52720
Comments
The same behavior happens with Also happens when capturing outside a loop: auto [_, enum_] = std::pair<int, int>{1, 2};
[_, enum_] { std::cout << _ << " " << enum_ << std::endl; }(); |
Last I recall, the above behaviour is actually correct for C++17 and GCC and MSVC simply implement it as a language extension, sadly leading to confusion. Specifically, Section 8.4.5.2 paragraph 8 states: "If a lambda-expression explicitly captures The workaround for your case in particular is to use an init list. Your lambda would be written as: [test = test, enum_thing = enum_thing] {
std::cout << test << " " << enum_thing << std::endl;
} (); If you'd want to capture it by reference instead, writing As @luizffgv notes however, it is definitely allowed in C++20 and that is indeed a missing feature/bug in clang. [1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4713.pdf |
Then, I'd argue that the bug is still valid.
The error should be
since the error is |
I'd argue that Section 9.6 paragraph 4 of n4892 states that, for a non-array type |
One could still improve the error message however. Its probably not ideal that one has to know specifically that a structured binding is not considered a variable. Simply stating that a structured binding can't be captured in C++17 would be better eg. (most likely it will just be a warning/error in the future that capturing a structured binding is a C++20 extension, but that remains to be seen) |
See also #48582 |
@llvm/issue-subscribers-clang-frontend |
This completes the implementation of P1091R3 and P1381R1. This patch allow the capture of structured bindings both for C++20+ and C++17, with extension/compat warning. In addition, capturing an anonymous union member, a bitfield, or a structured binding thereof now has a better diagnostic. We only support structured bindings - as opposed to other kinds of structured statements/blocks. We still emit an error for those. In addition, support for structured bindings capture is entirely disabled in OpenMP mode as this needs more investigation - a specific diagnostic indicate the feature is not yet supported there. Note that the rest of P1091R3 (static/thread_local structured bindings) was already implemented. at the request of @shafik, i can confirm the correct behavior of lldb wit this change. Fixes #54300 Fixes #54300 Fixes #52720 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D122768
The following toy code generates the following error.
This is something that I use constantly with Qt based code.
Testing with g++ 11.0 and the code compiles correctly
The text was updated successfully, but these errors were encountered: