2

I found an interesting case where valid(?) C++17 code fails when compiling with C++20. The simplest reproduction is a struct with an explicit default constructor. On C++17, this code works fine. In C++20, it fails to compile. Tested on Clang and GCC.

struct A {
    int val;
    A() = default;

    // When commented out: only works with C++17 or earlier
    // When uncommented: works up to C++20
    //A(int val) : val(val) { }
};

int main() {
    A a{4};
}

Does anyone know why this is occurring?

BWG
  • 2,238
  • 1
  • 20
  • 32

1 Answers1

4

In C++17, A is an aggregate. In C++20, the rules were changed so that any user-declared constructor prevents the class from being an aggregate, even if you default it on its first declaration. Therefore, in C++17, aggregate initialization can be used, while in C++20, the code is invalid because there is no constructor that is suitable for the initialization A a{4};.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • 1
    Now that I know the terminology of the change, I guess it is summed up much better in this answer https://stackoverflow.com/questions/57271400/why-does-aggregate-initialization-not-work-anymore-since-c20-if-a-constructor – BWG Jun 08 '22 at 01:36
  • 1
    @M.M You're right. I was wrong about this. I'll edit the answer. – Brian Bi Jun 08 '22 at 12:50