Consider the following piece of code, which is perfectly acceptable by a C++11 compiler:
#include <array>
#include <iostream>
auto main() -> int {
std::array<double, 0> A;
for(auto i : A) std::cout << i << std::endl;
return 0;
}
According to the standard § 23.3.2.8 [Zero sized arrays]:
1
Array shall provide support for the special caseN == 0
.
2
In the case thatN == 0
,begin() == end() ==
unique value. The return value of
data()
is unspecified.
3
The effect of callingfront()
orback()
for a zero-sized array is undefined.
4
Member functionswap()
shall have a noexcept-specification which is equivalent tonoexcept(true)
.
As displayed above, zero sized std::array
s are perfectly allowable in C++11, in contrast with zero sized arrays (e.g., int A[0];
) where they are explicitly forbidden, yet they are allowed by some compilers (e.g., GCC) in the cost of undefined behaviour.
Considering this "contradiction", I have the following questions:
Why the C++ committee decided to allow zero sized
std::array
s?Are there any valuable uses?