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
wuhhhhh, what is the difference
1
“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
Replying to
You can only capture variables directly, not expressions like “e.x”, but “x” is akin to a #define, not a variable w/ a type like “int&”, just a name that aliases that expression. “[&x = x]” = “add an extra member to the lambda object and initialise it with the expression ‘x’”.

