3

When using c++17 structure bindings and used these variables to be captured by a lambda function, clang fails to compile but it does work for MSVC and GCC. Here is the code (on godbolt):

std::tuple<int, double, long> f() { return { 1, 2., 3 }; }
auto [a,b,c] = f();
[a, &b, other = c]() {
    std::cout << a << b << other << std::endl;
}();

Clang returns an error for a and b: error: 'a' in capture list does not name a variable. I am not sure this is a valid code though. Can someone tell which compiler does things right?

Thank you

Vincent S.
  • 31
  • 1
  • 6
    [This is a duplicate.](https://stackoverflow.com/questions/46114214/lambda-implicit-capture-fails-with-variable-declared-from-structured-binding) Long story short: a technicality in the standard means that `a`, `b`, and `c` are just "aliases" for the components of the unnamed `std::tuple` variable bound by the structured binding, not variables/references themselves. Therefore, they can't be captured. This is an issue with the standard, so Clang is correct here. – HTNW Aug 20 '20 at 04:05

0 Answers0