std :: array< T,0>是否默认可构造,而T不是默认可构造? [英] Is `std::array<T, 0>` default constructible where `T` is not default constructible?
问题描述
考虑以下代码:
#include <array>
struct T
{
T() = delete;
};
int main()
{
std::array<T, 0> a;
a.size();
}
我们默认初始化一个0大小的数组.由于没有元素,因此不应调用T
的构造函数.
We default initialize a 0-sized array. Since there's no elements, no constructor of T
should be called.
但是, C语仍然要求T
是默认可构造的,而
However, Clang still requires T
to be default constructible, while GCC accepts the code above.
请注意,如果我们将数组初始化更改为:
Note that if we change the array initialization to:
std::array<T, 0> a{};
Clang 这次接受了.
非默认构造的T
是否阻止std::array<T, 0>
被默认构造?
Does non-default-constructible T
prevent std::array<T, 0>
from being default-constructible?
推荐答案
感谢@TC,正如他的 LWG 2157 ,在撰写本文时仍是一个未解决的问题.
Thanks to @T.C., as pointed out in his comment, it's addressed in LWG 2157, which is still an open issue as of this writing.
建议的决议添加了这一要点(强调我的意思):
The proposed resolution adds this bullet point (emphasis mine):
这种情况下未指定的数组内部结构应允许类似以下的初始化:
The unspecified internal structure of array for this case shall allow initializations like:
array<T, 0> a = { };
,并且所说的初始化必须有效,即使T不是默认可构造的.
and said initializations must be valid even when T is not default-constructible.
因此,很明显,预期的行为是即使std::array<T, 0>
默认构造值.
So it's clear that the intended behavior is to have std::array<T, 0>
default constructible even when T is not.
这篇关于std :: array< T,0>是否默认可构造,而T不是默认可构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!