Code:
struct A
{
private:
A() = default; // Version 1.
};
struct B : public A
{};
struct C
{
private:
C() {}; // Version 2.
};
struct D : public C
{};
int main()
{
B b; // Compiles under g++ 4.7.2
D d; // Compilation error under g++ 4.7.2
}
And two situations (with gcc 4.7.2):
D::D()
is private.Question: if I use default constructors, why do problems dissapear? If A
has private constructors, other classes can never create instances of A
, not even its derivated classes, irrespective of the "defaultness" of its constructor's implementation.
This smells like a GCC bug to me.
Neither the whole Clause 11 about member access control nor Section 8.4.2 about defaulted constructors mention anything about overriding the level of accessibility of defaulted constructors.
Besides, this code does not compile on Clang 3.2 and Intel ICC 13.0.
On the other hand, if you comment line X, the following line will not do what you think:
B b(); // This will declare a function that accepts no argument
// and returns a value of type B
If you remove the parentheses, you should see the problem popping up:
B b; // ERROR: Constructor of A is private
However, GCC 4.7.2 (incorrectly) raises no compilation error. It also seems this hasn't been fixed in the beta version of GCC 4.8.0 (as of build 20130127).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With