Andreas Loanjoe
Andreas Loanjoe

Reputation: 2399

How does std::array brace initialize?

Since array has no constructors or destructors and no public non-static member variables, how does array allow brace initialization? Trying to initialize the following type is not allowed:

template<typename T, std::size_t num>
class Array
{
    T data[num];
};

How can I write this type in a way that it is brace initializable without any constructors or destructors to keep the type trivially constructible and destructible, and without exposing the private array member?

Upvotes: 3

Views: 116

Answers (1)

Bathsheba
Bathsheba

Reputation: 234645

The constructor to std::array is implicitly declared.

Assuming std::array is defined as

template<
    class T,
    std::size_t N
> struct array;

It contains only one member, T[N], which has public access.

So therefore it's possible to initialise std::array by writing the appropriate syntax for aggregate initialization, i.e. by using braces.

Upvotes: 5

Related Questions