r/cpp • u/pavel_v • Apr 16 '26
C++26: Structured bindings in conditions
https://www.sandordargo.com/blog/2026/04/15/cpp26-structured-bindings-condition
60
Upvotes
r/cpp • u/pavel_v • Apr 16 '26
35
u/jedwardsol const & Apr 16 '26
That ordering (call foo, see if the temporary is true, then do the binding) is non-intuitive.
I prefer
which works since C++17 (https://godbolt.org/z/41ozW3PM3)
7
u/fdwr fdwr@github 🔍 Apr 16 '26 edited Apr 16 '26
What?.. 🤔 I'm sure I've used this before inside
ifstatements. (goes and checks...) Ah, I usedif (auto [x, y] = ...), notif (auto& [x, y] = ...)which indeed fails. That's cool to see related things being more consistent now (though, on the matter of consistency, why in the world did they abuse array notation[]to introduce a heterogeneous tuple instead of the usual{}or(), likeif (auto {a, z} = ...)- sigh).6
u/arthurno1 Apr 16 '26
Basically multiple values return and multiple value bind from Common Lisp, but one has to declare an explicit struct and polute the namespace instead of using an inline statement like in CL (values).
2
u/looneysquash Apr 16 '26
I haven't haven't gotten around to using that feature yet. (I am not writing C++ for work these days.)
But reading the docs, and as someone who uses Typescript daily, I was shocked that it only seems to support by order and not by name.
2
u/MrsGrayX Apr 16 '26
Isn‘t this easy to confuse with pattern matching in other languages? Aka “Can this temporary be unpacked into this structured binding?“.
-4
u/great_escape_fleur Apr 16 '26
How much more set-in-stone syntax are these “elders of the internet” going to invent?