Aggregate vs non-aggregate structs/classes

  aggregate-type, c++

I am trying to understand what an aggregate class/struct/union is: Here is from the C++ standard:

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal-initializers 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).

So I’ve written this for testing:

struct NonAggregate{
    virtual void f()const&{} // virtual member function makes the struct non-aggregate
};

struct Bar{}; // aggregate
struct X{}; // aggregate

struct Foo : Bar, X{
   // Foo() = default; // if un-comment out makes Foo non-aggregate
   Foo& operator =(Foo const&){return *this;} // ok
   ~Foo(){} // ok
   int x_ = 0; // has an initializer. ? still aggregate?
   Bar b_{}; // has an initializer. still aggregate?

private:
    static double constexpr pi_ = 3.14;
};

double constexpr Foo::pi_; // definition needed- (although it has an in-class intializer) if Foo::pi_ used outside of the class

int main(){

    std::cout << std::boolalpha << "Foo is an aggregate type: " << std::is_aggregate<Foo>::value << 'n';

}

The output is:

Foo is an aggregate type: true
  • So why If I default the synthesized default constructor Foo is no more an Aggregate? As long as I’ve not provided a user-defined one?

  • Also the standard says: no base classes (Clause 10) but my struct inherits multiply from two aggregate structs Bar and X but still n Aggregate??!

  • The standard says: no brace-or-equal-initializers for non-static data members (9.2) but I have initializers for non-static data members x_ and b_ but the compiler still consider Foo as an aggregate struct?? Thank you!

*P.S: I’ve used GCC and C++2a standard

Source: Windows Questions C++

LEAVE A COMMENT