std :: vector和std :: array之间是否存在聚合初始化差异?

时间:2018-03-18 08:20:15

标签: c++ gcc clang++ list-initialization aggregate-initialization

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::vectorstd::array之间是否存在聚合初始化差异?如果是,为什么?

按照标准,gcc应该像clang一样编译聚合吗?

godbolt.org/g/TRZHnu

0 个答案:

没有答案