0长度数组(或std::array)的用途是什么?

时间:2022-11-15 02:28:52

In C++11 it allows you to create a 0 length C array and std:array like this:

在c++ 11中,它允许创建一个0长度的C数组和std:数组,如下所示:

int arr1[0];
std::array arr2<int,0>;
  1. So I'm thinking what is the use of a array that doesn't have a space to store?
  2. 我在想一个没有空间存储的数组有什么用呢?
  3. Secondly what is the zero length array? If it is a pointer, where does it pointing to?
  4. 第二,什么是零长度数组?如果它是一个指针,它指向哪里?

1 个解决方案

#1


17  

Your first example is not standard C++ but is an extension that both gcc and clang allow, it is version of flexible arrays and this answer to the question: Are flexible array members really necessary? explains the many advantages of this feature. If you compiled using the -pedantic flag you would have received the following warning in gcc:

您的第一个示例不是标准的c++,而是gcc和clang都允许的扩展,它是灵活数组的版本,这个问题的答案是:灵活数组成员真的有必要吗?说明此特性的许多优点。如果您使用-pedantic标志进行编译,您将在gcc中收到以下警告:

warning: ISO C++ forbids zero-size array 'arr1' [-Wpedantic]

警告:ISO c++禁止零大小的数组‘arr1’[-Wpedantic]

and the following warning in clang:

以下是在clang的警告:

warning: zero size arrays are an extension [-Wzero-length-array]

警告:0大小的数组是扩展名[- w零长度数组]

As for your second case zero-length std::array allows for simpler generic algorithms without having to special case for zero-length, for example a template non-type parameter of type size_t. As the cppreference section for std::array notes this is a special case:

对于第二个大小写零长度的std::array允许使用更简单的通用算法,而不必使用特殊大小写表示零长度,例如size_t类型的模板非类型参数。作为std:::array的cppreference部分,这是一个特例:

There is a special case for a zero-length array (N == 0). In that case, array.begin() == array.end(), which is some unique value. The effect of calling front() or back() on a zero-sized array is undefined.

对于长度为0的数组(N = 0)有一个特殊的情况,在这种情况下,array.begin() == array.end()是某个唯一值。调用front()或back()对一个零大小的数组的效果没有定义。

It would also make it consistent with other sequence containers which can also be empty.

它还可以使它与其他序列容器保持一致,这些容器也可以是空的。

#1


17  

Your first example is not standard C++ but is an extension that both gcc and clang allow, it is version of flexible arrays and this answer to the question: Are flexible array members really necessary? explains the many advantages of this feature. If you compiled using the -pedantic flag you would have received the following warning in gcc:

您的第一个示例不是标准的c++,而是gcc和clang都允许的扩展,它是灵活数组的版本,这个问题的答案是:灵活数组成员真的有必要吗?说明此特性的许多优点。如果您使用-pedantic标志进行编译,您将在gcc中收到以下警告:

warning: ISO C++ forbids zero-size array 'arr1' [-Wpedantic]

警告:ISO c++禁止零大小的数组‘arr1’[-Wpedantic]

and the following warning in clang:

以下是在clang的警告:

warning: zero size arrays are an extension [-Wzero-length-array]

警告:0大小的数组是扩展名[- w零长度数组]

As for your second case zero-length std::array allows for simpler generic algorithms without having to special case for zero-length, for example a template non-type parameter of type size_t. As the cppreference section for std::array notes this is a special case:

对于第二个大小写零长度的std::array允许使用更简单的通用算法,而不必使用特殊大小写表示零长度,例如size_t类型的模板非类型参数。作为std:::array的cppreference部分,这是一个特例:

There is a special case for a zero-length array (N == 0). In that case, array.begin() == array.end(), which is some unique value. The effect of calling front() or back() on a zero-sized array is undefined.

对于长度为0的数组(N = 0)有一个特殊的情况,在这种情况下,array.begin() == array.end()是某个唯一值。调用front()或back()对一个零大小的数组的效果没有定义。

It would also make it consistent with other sequence containers which can also be empty.

它还可以使它与其他序列容器保持一致,这些容器也可以是空的。