TIL C++17 “structured bindings” introduce *names*, but not *variables*
<(ó ⩍ò)>
struct P { int x, y; };
int f(P p) {
auto [x, y] = p;
// Error: ’x’ in capture list does not name a variable
auto f = [&x, &y] { return x + y; };
return f(x, y);
}
Gotta use “[&x = x, &y = y]”
Conversation
Replying to
“x” & “y” in “auto [x, y]” aren’t variables, nor references; they’re names referring to members of a variable that’s implicitly created & copied from “p”. So you can have e.g. structured bindings that refer to bitfields even tho there’s no such thing as “reference-to-bitfield”.
1
1
You can’t capture them for the same reason you can’t capture what they desugar to, namely:
auto e = p;
// More clearly an error:
auto f = [&e.x, &e.y] { … };
1
Show replies

