c++g++list-initialization

Why does list-Initialized object using default constructor compile in gcc9 but not in gcc5.1?


Why does following code compiles in gcc9 but not in gcc5.1?

struct AAA {

    int xxx  = 1;
};

int main() {
        AAA p;
        new AAA{p};                                                                                                                                                                                                             
}

Error when compiled with gcc5.1 -

/home/genstor/cpp/test.cpp:11:18: error: cannot convert 'AAA' to 'int' in initialization
         new AAA{p};
                 ^

What have I found so far?

I have seen that using list-initializer for structs where default constructor's is used have some problems compiling in 4.8.1 from here, but couldn't relate it with this as it is 5.1. Any help in understanding this more is appreciated.

CMD: g++ ~/cpp/test.cpp --std=c++14

Repro link - https://godbolt.org/z/hEz95dq4G


Solution

  • gcc5.2 resolves issue 51747

    which relate to a standard defect reports #1467

    Change 8.5.4 [dcl.init.list] paragraph 3 as follows:

    List-initialization of an object or reference of type T is defined as follows:

    • If T is a class type and the initializer list has a single element of type cv T or a class type derived from T, the object is initialized from that element.

    • Otherwise, if T is an aggregate...

    I'm not sure why c++11 compiles though. the bug report for gcc seems to indicate it would not compile either.