Search code examples
c++c++11stdarray

std::array member functions empty(), max_size() - useless but for consistency?


Are these member functions as useless as they seem and exist just to provide consistency with other containers?

For example:

std::array<int, 4> array1;  // size of 4 (but no elements initialized)
std::array<int, 0> array2;  // size of zero.

array1.empty();  // false - not empty even though no elements are initialized
array2.empty();  // true - empty and no way to add elements

array1.size();      // room for four now
array1.max_size();  // room for four forever

array2.size();      // no room for anything now
array2.max_size();  // ... or ever

The answer to "Why is std::array< T, 0 > not empty?" deals with a zero "size" param and a non-zero return from sizeof(), i.e., it does take up space even when empty. But that is not what I am asking.


Solution

  • Yes, they are only there for consistency, allowing easier template specialisation.
    Still your comment about std::array<int, 4> starting with no elements is wrong: It's a dressed up int[4], now and forever.
    To your aside, per standard no most-derived C++ object is ever smaller than 1.