In the following example, struct A
does not have default constructor. So both struct B
and struct C
inherited from it cannot get compiler-generated default constructor:
struct A {
A(int) {}
};
struct B : A {
B() = default; //#1
};
struct C : A {
C();
};
C::C() = default; //#2
#1. In struct B
, defaulted default constructor is declared inside the class, and all compilers accept it, with only Clang showing a warning saying that explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]
#2. But defaulted default constructor declared outside of struct C
generates a compiler error. Demo: https://gcc.godbolt.org/z/3EGc4rTqE
Why does it matter where the constructor is defaulted: inside or outside the class?