Is `std::array` default constructible where `T` is not default constructible?

后端 未结 3 1905
谎友^
谎友^ 2021-01-08 00:18

Consider the code below:

#include 

struct T
{
    T() = delete;
};

int main()
{
    std::array a;
    a.size();
}
<
3条回答
  •  梦毁少年i
    2021-01-08 00:32

    Since there's no elements, no constructor of T should be called.
    Does non-default-constructible T prevent std::array from being default-constructible?

    The standard doesn't specify what layout std::array should have for us to answer that. The zero sized array specialization is only said to behave as follows:

    [array.zero]

    1 array shall provide support for the special case N == 0.
    2 In the case that N == 0, begin() == end() == unique value. The return value of data() is unspecified.
    3 The effect of calling front() or back() for a zero-sized array is undefined.
    4 Member function swap() shall have a non-throwing exception specification.

    The behavior you note is most probably due to differences in implementation alone.

提交回复
热议问题