在 C++17 中声明大小为 0 的数组是否合法?
Is it legal in C++17 to declare an array of size 0?
int main()
{
int n[0];
}
以上代码适用于 Clang 4.0。
然而,http://en.cppreference.com/w/cpp/language/array 表示:
The size of an array must be a value greater than zero.
根据 C++17 标准声明大小为 0 的数组是否合法?
它是 "legal" 只是因为它是 gcc 和 clang 的扩展。如果您使用 -pedantic
进行编译,那么您会发现您正在做的事情不适用于 ISO C++ 标准
Is it legal to declare an array of size 0 as per the C++17 standard?
不,C++17 中没有任何更改以允许大小为零的数组。根据 C++17 草案 [dcl.array]/1
In a declaration T D where D has the form
D1 [ constant-expressionopt] attribute-specifier-seqopt
[...]If the constant-expression is present, it shall be a converted constant expression of type std::size_t and its value shall be greater than zero.[...]
强调我的
您在这里看到的是允许您编译代码的非标准编译器扩展。
您可以使用 -pedantic
编译器标志禁用这些扩展。
int main()
{
int n[0];
}
以上代码适用于 Clang 4.0。
然而,http://en.cppreference.com/w/cpp/language/array 表示:
The size of an array must be a value greater than zero.
根据 C++17 标准声明大小为 0 的数组是否合法?
它是 "legal" 只是因为它是 gcc 和 clang 的扩展。如果您使用 -pedantic
进行编译,那么您会发现您正在做的事情不适用于 ISO C++ 标准
Is it legal to declare an array of size 0 as per the C++17 standard?
不,C++17 中没有任何更改以允许大小为零的数组。根据 C++17 草案 [dcl.array]/1
In a declaration T D where D has the form
D1 [ constant-expressionopt] attribute-specifier-seqopt
[...]If the constant-expression is present, it shall be a converted constant expression of type std::size_t and its value shall be greater than zero.[...]
强调我的
您在这里看到的是允许您编译代码的非标准编译器扩展。
您可以使用 -pedantic
编译器标志禁用这些扩展。