c++c++11inheritancedeleted-functions

Force compiler to emit error on move constructor not defined with a base class deleted copy ctor


Consider a base class which prevents copy construction and copy assignment like this:

class NonCopyable {
    public:
        NonCopyable() = default;
        ~NonCopyable() = default;

        NonCopyable(NonCopyable const&)                 = delete;
        NonCopyable& operator=(NonCopyable const&)      = delete;
};

Our developers can now include this class, and use it to disable copy for inherited classes, like this:

class CopyTest : public NonCopyable {
    public:
        CopyTest() {
            std::cout << "copy test created" << std::endl;
        }

        ~CopyTest() {
            std::cout << "copy test deleted" << std::endl;
        }
};

When I try to use the CopyTest class:

CopyTest ct, ct1(ct);

or

CopyTest ct;
CopyTest ct1 = ct2;

The compiler emit an error : use of deleted function xxx (where xxx is my deleted copy ctor, or copy operator)

Then, if I want to std::move a CopyTest object :

CopyTest ct;
CopyTest ct1 = std::move(ct);

The compiler emit the same error (use of deleted function xxx - where xxx remains my copy ctor or assignment operator).

If I remind correctly, it's because the developer has not defined the proper move ctor/assignment operator.

Is it possible to force the compiler to tell the developer of the CopyTest class that the move error is here because he has not defined the proper move ctor/assignment operator, and not because the copy ctor/assignment operator is deleted on the base class?

Platform:
Debian 9
GCC 6.3.0

Compilation flags: -fpermissive -ggdb -std=c++11


Solution

  • add

        NonCopyable(NonCopyable &&)                 = delete;
        NonCopyable& operator=(NonCopyable &&)      = delete;
    

    that now complains about the base class move ctor being deleted.