initializer_list with auto 包含多个表达式
initializer_list with auto contains multiple expressions
相当简单的问题,
auto x11 {1,2,3,4};
auto x1 = {1,2,3,4};
auto x22 {1.0, 2.25, 3.5};
auto x2 = {1.0, 2.25, 3.5};
据我了解,有无=
应该没有区别。但是,使用 llvm/clang 6.0.0(使用 --std=c++17),我得到:
main1.cpp:35:17: error: initializer for variable 'x11' with type 'auto' contains multiple
expressions
auto x11 {1,2,3,4};
~~~~~~~~ ^
main1.cpp:37:20: error: initializer for variable 'x22' with type 'auto' contains multiple
expressions
auto x22 {1.0, 2.25, 3.5};
来自 Stroustroup 的 C++ 书,第 162 页:
auto x1 {1,2,3,4}; // x1 is an initializer_list<int>
auto x2 {1.0, 2.25, 3.5 }; // x2 is an initializer_list of<double>
那么,里面没有 = 真的有问题吗?
auto type deduction changed since N3922的规则。 (这在 C++14 中被认为是一个缺陷)。
In direct-list-initialization (but not in copy-list-initalization),
when deducing the meaning of the auto from a braced-init-list, the
braced-init-list must contain only one element, and the type of auto
will be the type of that element:
auto x1 = {3}; // x1 is std::initializer_list<int>
auto x2{1, 2}; // error: not a single element
auto x3{3}; // x3 is int
// (before N3922 x2 and x3 were both std::initializer_list<int>)
所以在 N3922 之前,您样本中的所有变量都工作正常并且类型为 std::initializer_list<int>
。但是从 N3922 开始,对于直接初始化(即对于 x11
和 x22
),大括号初始化器必须只包含一个元素(并且它们的类型将是元素的类型),然后代码变得错误 -成立。
相当简单的问题,
auto x11 {1,2,3,4};
auto x1 = {1,2,3,4};
auto x22 {1.0, 2.25, 3.5};
auto x2 = {1.0, 2.25, 3.5};
据我了解,有无=
应该没有区别。但是,使用 llvm/clang 6.0.0(使用 --std=c++17),我得到:
main1.cpp:35:17: error: initializer for variable 'x11' with type 'auto' contains multiple
expressions
auto x11 {1,2,3,4};
~~~~~~~~ ^
main1.cpp:37:20: error: initializer for variable 'x22' with type 'auto' contains multiple
expressions
auto x22 {1.0, 2.25, 3.5};
来自 Stroustroup 的 C++ 书,第 162 页:
auto x1 {1,2,3,4}; // x1 is an initializer_list<int>
auto x2 {1.0, 2.25, 3.5 }; // x2 is an initializer_list of<double>
那么,里面没有 = 真的有问题吗?
auto type deduction changed since N3922的规则。 (这在 C++14 中被认为是一个缺陷)。
In direct-list-initialization (but not in copy-list-initalization), when deducing the meaning of the auto from a braced-init-list, the braced-init-list must contain only one element, and the type of auto will be the type of that element:
auto x1 = {3}; // x1 is std::initializer_list<int> auto x2{1, 2}; // error: not a single element auto x3{3}; // x3 is int // (before N3922 x2 and x3 were both std::initializer_list<int>)
所以在 N3922 之前,您样本中的所有变量都工作正常并且类型为 std::initializer_list<int>
。但是从 N3922 开始,对于直接初始化(即对于 x11
和 x22
),大括号初始化器必须只包含一个元素(并且它们的类型将是元素的类型),然后代码变得错误 -成立。