簡體   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