Is there a reason for zero sized std::array in C++11?

孤者浪人 提交于 2019-12-12 07:39:03

问题


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 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 noexcept-specification which is equivalent to noexcept(true).

As displayed above, zero sized std::arrays 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::arrays?

  • Are there any valuable uses?


回答1:


If you have a generic function it is bad if that function randomly breaks for special parameters. For example, lets say you could have a template function that takes N random elements form a vector:

template<typename T, size_t N>
std::array<T, N> choose(const std::vector<T> &v) {
   ...
}

Nothing is gained if this causes undefined behavior or compiler errors if N for some reason turns out to be zero.

For raw arrays a reason behind the restriction is that you don't want types with sizeof T == 0, this leads to strange effects in combination with pointer arithmetic. An array with zero elements would have size zero, if you don't add any special rules for it.

But std::array<> is a class, and classes always have size > 0. So you don't run into those problems with std::array<>, and a consistent interface without an arbitrary restriction of the template parameter is preferable.




回答2:


One use that I can think of is the return of zero length arrays is possible and has functionality to be checked specifically.

For example see the documentation on the std::array function empty(). It has the following return value:

true if the array size is 0, false otherwise.

http://www.cplusplus.com/reference/array/array/empty/

I think the ability to return and check for 0 length arrays is in line with the standard for other implementations of stl types, for eg. Vectors and maps and is therefore useful.




回答3:


As with other container classes, it is useful to be able to have an object that represents an array of things, and to have it possible for that array to be or become empty. If that were not possible, then one would need to create another object, or a managing class, to represent that state in a legal way. Having that ability already contained in all container classes, is very helpful. In using it, one then just needs to be in the habit of relating to the array as a container that might be empty, and checking the size or index before referring to a member of it in cases where it might not point to anything.




回答4:


There are actually quite a few cases where you want to be able to do this. It's present in a lot of other languages too. For example Java actually has Collections.emptyList() which returns a list which is not only size zero but cannot be expanded or resized or modified.

An example usage might be if you had a class representing a bus and a list of passengers within that class. The list might be lazy initialized, only created when passengers board. If someone calls getPassengers() though then an empty list can be returned rather than creating a new list each time just to report empty.

Returning null would also work for the internal efficiency of the class - but would then make life a lot more complicated for everyone using the class since whenever you call getPassengers() you would need to null check the result. Instead if you get an empty list back then so long as your code doesn't make assumptions that the list is not empty you don't need any special code to handle it being null.



来源:https://stackoverflow.com/questions/24152435/is-there-a-reason-for-zero-sized-stdarray-in-c11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!