在std::array
中,我们可以简单地这样表述
std::array<std::array<char,10>, 2> arr_gcc{
"abcdefghi", "abcdefghi"
};
或者甚至在与 -Wall 一起铿锵声中,它们只需要更明确的表达,必须添加更多像这样的子对象大括号
std::array<std::array<char,10>, 2> arr_clang_wall_favor{{
{"abcdefghi"}, {"abcdefghi"}
}};
说到std::vector
, 一种方法是像他的一样显式地构造每个子对象。
// This works in both clang and gcc
std::vector vec{
std::array<char,10>{"abcdefghi"}, std::array<char,10>{"abcdefghi"}
};
如果我们尝试像这样汇总
std::vector<std::array<char,10> > vec_clang{{
{"abcdefghi"}, {"abcdefghi"}
}};
它仅在clang中有效,但在gcc中是错误的
<source>: In function 'int main()':
<source>:21:6: error: no matching function for call to 'std::vector<std::array<char, 10>, std::allocator<std::array<char, 10> > >::vector(<brace-enclosed initializer list>)'
}};
^
In file included from /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/vector:64:0,
from <source>:2:
/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/bits/stl_vector.h:411:2: note: candidate: template<class _InputIterator, class> std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&)
vector(_InputIterator __first, _InputIterator __last,
^~~~~~
/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/bits/stl_vector.h:411:2: note: template argument deduction/substitution failed:
<source>:21:6: note: candidate expects 3 arguments, 1 provided
}};
^
std::vector
和之间是否存在聚合初始化差异std::array
?如果是,为什么?
按照标准,gcc 是否应该像 clang 那样编译该聚合?