r/cpp • • Apr 16 '26

C++26: Structured bindings in conditions

https://www.sandordargo.com/blog/2026/04/15/cpp26-structured-bindings-condition
60 Upvotes

27 comments sorted by

View all comments

Show parent comment

4

u/_Noreturn Apr 16 '26

it can't be () nor a {} due to vexing parse.

auto (a) = tuple;
auto {a} = tuple;
// auto can be a cast and copies 

1

u/fdwr fdwr@github 🔍 Apr 16 '26

If the most vexing parse was a Chinese finger trap, rather than dislodge our fingers, we would invent novel new pens that worked with trapped fingers 😉. 

1

u/_Noreturn Apr 16 '26

It shouldn't have required auto keyword.

{a,b,c} = tuple;

I don't see the reason for why a keyword is even needed

2

u/fdwr fdwr@github 🔍 Apr 16 '26

Since it's already legal in functions to introduce braces arbitrarily for scoping, that would certainly require some lookahead to distinguish between nested statements. Having a leading keyword helps the compiler (and it's more consistent with the ordinary case of introducing local fields).

1

u/_Noreturn Apr 16 '26
void func()
{
   int a,b,c;
   { a,b,c}; // invalid
   {a,b,c;}; // valid
}

The lookahead would be whether there is a semicolon inside the braces