Why is std::array< T> not empty?

前端 未结 1 1168

Given any std::array< T, 0 >, why is it not empty? I mean \"empty\" as in:

 std::is_empty< std::array< int, 0 > >::value


        
相关标签:
1条回答
  • 2020-12-07 00:52

    The standard doesn't say anything about whether tuple or array should be empty, what you're seeing are implementation details, but there's no reason to make tuple<> non-empty, whereas there is a good reason for array<T, 0> being non-empty, consider:

    std::array<int, sizeof...(values)> = { { values... } };
    

    When the parameter pack is empty you'd get:

    std::array<int, 0> = { { } };
    

    For the initializer to be valid the object needs a member, which cannot be int[0] because you can't have zero-sized arrays as members, so a possible implementation is int[1]

    An implementation doesn't have to special case the whole array, it can just do:

    T m_data[N == 0 ? 1 : N];
    

    and all other members work exactly the same way (assuming end() is defined as begin()+N)

    0 讨论(0)
提交回复
热议问题