简体   繁体   中英

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

I found that there is a template partial specialisation in 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)];
    //...
}

So what is the purpose of implementing std::array<T, 0> ?

Thanks very much!

The reason is, simply, uniformity. When you're writing templates it's much easier to be able to always write std::array<Ty, N> than to have to write a special case when N is 0. That kind of uniformity comes up often: new int[0] , operator new(0) , std::malloc(0) , for (int i = 0; i < N; ++i) when N is 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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