g++ 4.9 rejects valid aggregate initialization in C++14

后端 未结 1 1237
半阙折子戏
半阙折子戏 2020-12-10 20:43

Consider this code:

struct S
{
    int x;
    double y = 1.1;
};

int main()
{
    S s = {0};
}

According to the C++14 standard, § 8.5.1/7<

1条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 21:16

    You are correct, this is valid C++14; however, in C++11 a class with in class member initializers was not an aggregate and so this is not valid in C++11.

    The issue as I noted in my answer to the above question and I realized later after I made my initial comment is that gcc did not support this change until 5.0 (see it live):

    G++ now supports C++14 aggregates with non-static data member initializers.

    struct A { int i, j = i; };
    A a = { 42 }; // a.j is also 42
    

    0 讨论(0)
提交回复
热议问题