c – `std :: array`默认是可构造的,其中`T`不是默认构造的吗?
作者:互联网
请考虑以下代码:
#include <array>
struct T
{
T() = delete;
};
int main()
{
std::array<T, 0> a;
a.size();
}
我们默认初始化一个0大小的数组.由于没有元素,因此不应该调用T的构造函数.
但是,Clang仍然要求T是默认构造,而GCC接受上面的代码.
请注意,如果我们将数组初始化更改为:
std::array<T, 0> a{};
Clang这次接受了.
非默认可构造的T是否阻止std :: array< T,0>从默认可构造?
解决方法:
Since there’s no elements, no constructor of T should be called.
Does non-default-constructible T preventstd::array<T, 0>
from being default-constructible?
该标准没有指定什么布局std :: array< T,0>应该让我们回答这个问题.零大小的数组专门化只表现如下:
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.
您注意到的行为很可能是由于实施方面的差异.
标签:c,c11,arrays,language-lawyer 来源: https://codeday.me/bug/20191007/1866896.html