Why does std::array not have an constructor that takes a value for the array to be filled with?

rubenvb picture rubenvb · Jul 29, 2013 · Viewed 38.3k times · Source

Is the absence of

std::array<T,size>::array(const T& value);

an oversight? It seems mighty useful to me, and dynamic containers (like std::vector) do have a similar constructor.

I am fully aware of

std::array<T,size>::fill(const T& value);

but that is not a constructor, and the memory will be zeroed out first. What if I want all -1's like this guy?

Answer

Mike Seymour picture Mike Seymour · Jul 29, 2013

std::array is, by design, an aggregate, so has no user-declared constructors.

As you say, you could use fill after default constructing. Since it's an aggregate, default construction won't zero the memory, but will leave it uninitialised (if the contained type is trivially initialisable).