xmllmx
xmllmx

Reputation: 42379

Is an array of any type always an aggregate?

class A 
{
public:
    A(){}

private:
    int i;
};

A a[8];

The C++11 standard 8.5.1.1 says:

"An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equalinitializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3)."

As a is an array, is a an aggregate? I think not, but the standard says yes.

Upvotes: 12

Views: 263

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477398

Yes, A[8] is an aggregate type, even though A is not.

The notion of aggregate is not transitive, unlike some other related notions (such as "trivially copyable").

Loosely speaking, being an aggregate only impacts the type's initialization, and thus it does not need to be transitive. You can say A a[2] = { A('x', true), A(1, 2, 3) }; without needing to put restrictions on the nature of A. By contrast, notions like trivial copyability relate to a class's memory layout and thus by their very nature must be transitive.

Upvotes: 15

Related Questions