我发现 std::array for 中有一个模板部分特化std::array<T, 0>。
template <typename T>
struct array<T, 0> {
//...
typedef typename conditional<is_const<_Tp>::value, const char,
char>::type _CharType;
struct _ArrayInStructT { _Tp __data_[1]; };
alignas(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)];
//...
}
Run Code Online (Sandbox Code Playgroud)
那么实施的目的是std::array<T, 0>什么?
非常感谢!
原因很简单,一致性。在编写模板时,始终编写std::array<Ty, N>比必须编写特殊情况 when Nis 0容易得多。这种一致性经常出现:new int[0], operator new(0), std::malloc(0), for (int i = 0; i < N; ++i)when Nis 0。