7

I have class base which only contains private default constructor and public deleted copy constructor, and nothing else.

class base {
private:
    base() = default;

public:
    base(const base&) = delete;
};

If I try to inherit from base and create an instance of class derived as below, g++ 4.8.2 doesn't compile my code but VC++ 2013 does.

class derived : public base {
private:
    derived() = default;
};

derived x;

So, is it a bug in g++ or VC++ 2013 just ignored something?

Here's the complete code...

class base {
private:
    base() = default;

public:
    base(const base&) = delete;
};

class derived : public base {
private:
    derived() = default;
};

derived x;

int main() { 
}

... and g++ error message.

main.cpp:12:5: error: 'constexpr derived::derived()' is private
     derived() = default;
     ^
main.cpp:15:9: error: within this context
 derived x;
         ^
main.cpp: In constructor 'constexpr derived::derived()':
main.cpp:3:5: error: 'constexpr base::base()' is private
     base() = default;
     ^
main.cpp:12:5: error: within this context
     derived() = default;
     ^
main.cpp: At global scope:
main.cpp:15:9: note: synthesized method 'constexpr derived::derived()' first required here
 derived x;
         ^
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
so61pi
  • 780
  • 2
  • 7
  • 21
  • 8
    I would argue it's a bug in VS2013. The constructor is private and therefore you can't create an instance of the class. – Some programmer dude Apr 15 '14 at 16:05
  • But if class derived doesn't inherit from class base, g++ will let it compile. Maybe default constructor marked default is just like implicitly declared default constructor. – so61pi Apr 15 '14 at 16:12
  • 4
    @so61pi The fact that g++ will not diagnose that case is [GCC bug 56429](http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56429). – Casey Apr 15 '14 at 16:17
  • @Casey Oh thank god, my idea about Explicitly private constructor and Implicitly-declared default constructor was wrong :D . – so61pi Apr 15 '14 at 16:24
  • But for now, like g++, VC++ 2013 also ignores Explicitly default private constructor. – so61pi Apr 15 '14 at 16:26
  • Just tested, Clang 3.4 doesn't have this compiler-bug. – so61pi Apr 15 '14 at 16:53

1 Answers1

5

You are misreading the error, it is telling you that the default constructor for derived is not accessible (is private) so that you cannot use that to create an object of that type. Now making it public at the derived level won't help, since the base constructor is also private and thus cannot be used within the constructor of derived.

Why do you want those constructors to be private?

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489