c++initializationlist-initializationaggregate-initialization

c++11 - list-initialization of an aggregate from an aggrrgate


On this page of the cppreference.com I read the following:

If T is an aggregate class and the braced-init-list has a single element of the same or derived type (possibly cv-qualified), the object is initialized from that element (by copy-initialization for copy-list-initialization, or by direct-initialization for direct-list-initialization).

But this page states this:

An aggregate is one of the following types:

  • array type
  • class type (typically, struct or union), that has
    no private data members
    no user-provided, inherited, or explicit constructors

But if an aggregate class has no user defined constructors, how it could be initialized in a manner stipulated above? I mean, it is not saying that aggregate members are getting their values from the members of the initializer, it explicitly mentions the constructor from the initializer.


PS1 It seems like this one is a good exemplification:

#include <iostream>
#include <type_traits>
class A {
    public:
        int a;
        int b;
        //this one does nothing to the code below:
        //A()         = delete; 
        //this one does BOOM
        //A(A &other) = delete;  
    private:
};

int 
main()
{
    if( !std::is_aggregate<A>::value ) {
        std::cout << "A is not aggregate!" << std::endl;
    }
    A a = { 1, 2 };
    A b = { a };

    std::cout << "a is " << a.a << " " << a.b << std::endl;
    std::cout << "b is " << b.a << " " << b.b << std::endl;
}

So, copy constructor it is, I guess.


PS2

I have the same behavior when compiling without std::is_aggregate and with c++11 flag on.


Solution

  • of the same or derived type

    That means that default copy-constructor will be used. That's why there is no contradiction between these two rules