Search code examples
c++classconstructorc++20

Automatic constructor inheritance in C++20


I just have this code, and I wonder why this code compiles in C++20 and later, but it doesn't compile in C++17 and earlier.

struct B {
  B(int){};
};

struct D : B {
};

int main() {

  D d = D(10);

}

I know that inheriting constructors is a C++11 feature. But class D doesn't inherit the B::B(int) constructor, even though this line D d = D(10); compiles. My question is, why does it compile only in C++20 and not in C++17? Is there a quote to the C++ standard that applies here?

I am using g++11.2.0.


Solution

  • C++20 added the ability to initialize aggregates using parentheses; see P0960. Previously, you could have initialized d using D d{10};; now you can do the same thing with parentheses instead of braces. The class D does not implicitly inherit constructors from B.