c++c++11move-constructordefault-copy-constructor

Uncopyable class with automatic default and move constructors


I want to make some classes use automatically generated constructors, but be non-copyable (but still movable). Currently I'm doing it like this:

class A
{
public:
    A() = default;
    A(const A&) = delete;
    A(A&&) = default;
    A& operator=(const A&) = delete;
    A& operator=(A&&) = default;
}

I wonder if it's really necessary to be so explicit. What if I wrote it like this:

class A
{
    A(const A&) = delete;
    A& operator=(const A&) = delete;
}

Would it still work the same? What is the minimal set of defaults and deletes for other cases - non-copyable non-movable class, and class with virtual destructor?

Is there any test code I can use to quickly see which constructors are implicitly created?


Solution

  • This will not work because no default constructor will be automatically created for you. No default constructor will be created because you have declared a copy constructor. It is defined as deleted, but it is user-declared nonetheless, so there is no implicitly defaulted default constructor.

    The condensed rules for implicitly created constructors are: