Private and standard constructor in C ++ 11 and gcc

the 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):

  • If I compile this code (with version 1 of constructor A), there is no problem.
  • If I use the second constructor, gcc tells me what D::D()is private.

Question: if I use the default constructors, why do the problems go away? If it Ahas private constructors, other classes will never be able to create instances A, even its derivative classes, regardless of the "failure" of its constructor implementation.

+5
source share
1 answer

It smells like a GCC bug .

11 , 8.4.2 .

, Clang 3.2 Intel ICC 13.0.

, X, , :

B b(); // This will declare a function that accepts no argument
       // and returns a value of type B

, , :

B b; // ERROR: Constructor of A is private

GCC 4.7.2 () . , - GCC 4.8.0 ( 20130127).

+5

All Articles