9

This code will not compile with gcc 4.7.0:

class Base
{
public:
    Base(const Base&) = delete;
}; 

class Derived : Base
{
public:
    Derived(int i) : m_i(i) {}

    int m_i;
};

The error is:

c.cpp: In constructor `Derived::Derived(int)´:
c.cpp:10:24: error: no matching function for call to `Base::Base()´
c.cpp:10:24: note: candidate is:
c.cpp:4:2: note: Base::Base(const Base&) <deleted>
c.cpp:4:2: note:   candidate expects 1 argument, 0 provided

In other words, the compiler does not generate a default constructor for the base class, and instead tries to call the deleted copy constructor as the only available overload.

Is that normal behavior?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
kounoupis
  • 329
  • 1
  • 10
  • 1
    Yes, because if any constructor is *user-declared*, the default-constructor is suppressed. – Xeo Aug 16 '13 at 17:04
  • 1
    By why is deleting a constructor considered to be a declaration of a constructor? This is counter intuitive. – kounoupis Aug 18 '13 at 11:37

1 Answers1

16

C++11 §12.1/5 states:

A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4).

Your Base(const Base&) = delete; counts as a user-declared constructor, so it suppresses generation of the implicit default constructor. The workaround is of course to declare it:

Base() = default;
Casey
  • 41,449
  • 7
  • 95
  • 125
  • 1
    Why would a deleted constructor count as a declaration of a constructor? This does not make sense to me. A deleted constructor explicitly states the intention to not allow the definition of such constructor (defaulted or not). – kounoupis Aug 19 '13 at 13:49
  • 4
    I found this: [ISO/IEC JTC1 SC22 WG21 N2346](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm#delete) that states that all lookup and overload resolution happens *before* the deleted definition is noted, hence this answer is correct. – kounoupis Aug 19 '13 at 14:09