繁体   English   中英

std::array 的目的是什么<t, 0> ?</t,>

[英]What is the purpose of std :: array<T, 0>?

我发现 std::array 中有一个用于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)];
    //...
}

那么实现std::array<T, 0>的目的是什么?

非常感谢!

原因很简单,就是一致性。 当您编写模板时,总是能够编写std::array<Ty, N>比编写N为 0 时的特殊情况要容易得多。这种统一性经常出现: new int[0] , operator new(0) , std::malloc(0) , for (int i = 0; i < N; ++i)N为 0 时。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM