Deleted copy constructor results in deleted default constructor

2.3k views Asked by At

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?

1

There are 1 answers

2
Casey On BEST ANSWER

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;