Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 417
rubenvb Avatar asked Jul 29 '13 12:07

rubenvb


People also ask

Does std :: array default constructor?

std::array::arrayFor elements of a class type this means that their default constructor is called. For elements of fundamental types, they are left uninitialized (unless the array object has static storage, in which case they are zero-initialized).

Does std :: array initialize?

std::array contains a built-in array, which can be initialized via an initializer list, which is what the inner set is. The outer set is for aggregate initialization.

What is the point of std :: array?

std::array is merely a wrapper around the C-style fixed arrays. to provide type-safety with useful interfaces. Stack-allocation implies that the data for the array is stored in the object itself.

Is std :: array trivial?

std::array does satisfy all the requirements of being a trivial, standard-layout class template. So the answer to your question is yes.


1 Answers

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).

like image 132
Mike Seymour Avatar answered Sep 20 '22 05:09

Mike Seymour