Why do C++17 structured bindings not use { }?
I found the original proposal for *C++ structured bindings here. It proposes a way to easily bind multiple return values, i.e.:
auto {a, b} = minmax(data);
But now I see that everyone points to the C++17/C++1z proposal syntax of
auto [a, b] = minmax(data);
Now that I learned "lists are written { like, this }" there comes a new list-syntax? Why? What is the problem with curly braces here?
Solution 1:
This is still under debate. It's difficult to be certain which syntax will be least confusing given how many uses there are for [] and {} already.
There's also the risk that "least confusing" and "easiest to parse" will be in conflict.
Solution 2:
The National Bodies from Spain and US have proposed to change back to the {}
syntax because (P0488R0):
The “structured bindings” proposal originally used braces “{}” to delimit binding identifiers. Those delimiters were changed to brackets “[]” under the assertion that they didn’t introduce any syntactic problem. However, they turned out to introduce syntactic ambiguity with attributes and lambdas. In the light of various suggested fixes, it appears the original syntax is more adequate.
Therefore, there still remains the possibility of ending up having the original syntax for C++17 (which I strongly believe is preferred by most users).
Update from this trip report:
The original proposal for decomposition declarations used the syntax
auto {a, b, c};
that was changed at the last meeting toauto [a, b, c]
. This change was fairly controversial, and several comments asked to change it back to{}
(while others encouraged keeping the[]
). There are technical arguments on both sides (the[]
syntax can conflict with attributes once you start allowing nested decompositions; the{}
syntax can conflict with uniform initialization if you throw Concepts into the mix and allow using a concept-name instead ofauto
), so in the end it’s largely a matter of taste. The clang implementers did report that they tried both, and found the ambiguities to be easier to work around with[]
. In the end, there was no consensus for a change, so the status quo ([]
syntax) remains.