c++ - 为什么 C++17 结构化绑定(bind)不使用 {}?

标签 c++ c++17 structured-bindings

我找到了 *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 to auto [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 of auto), 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/

相关文章:

c++ - 从C使用C++对象

c++ - 为什么 reinterpret_cast 在危险时仍在使用

c++ - 无法识别 NSIS 插件功能

c++ - c++ 中是否有任何日期时间模块

c++ - 具有约束/概念的类模板特化

c++ - 为什么结构化绑定(bind)在结构上不能按预期工作?

c++ - 由于从未运行的函数调用,程序花费的时间比应有的时间长很多

c++ - C++17 中结构化绑定(bind)引入的标识符有哪些类型?

还包括现有变量的 C++17 结构化绑定(bind)

c++ - 静态断言失败 : variant must have no reference alternative