我找到了 *C++ 结构化绑定(bind)的原始提案 here .它提出了一种轻松绑定(bind)多个返回值的方法,即:
auto {a, b} = minmax(data);
但是现在看到大家都指向了C++17/C++1z的提案语法
auto [a, b] = minmax(data);
既然我学会了“列表是写成 { 就像,这个 }”,那么有一个新的列表语法吗?为什么?这里的大括号有什么问题?
最佳答案
西类牙和美国的国家机构提议改回 {}
语法,因为 (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.
因此,仍有可能最终获得 C++17 的原始语法(我坚信这是大多数用户的首选)。
更新从此 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.
关于c++ - 为什么 C++17 结构化绑定(bind)不使用 {}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40333000/