Does the compiler generate the move operations (not default) in case of a default destructor?

Does the compiler generate the move operations (not default) in case of a default destructor?

A relatively same question is asked before, but the code snippet is different, which I believe makes this question unique.

Does the compiler generate the move operations in the following scenario?

class User {
public:
    virtual ~User() = default;
    User( User&& );
    User& operator=( User&& );
    User( User const& );
    User& operator=( User const& );
};

If no, what is the difference between the last scenario and following case? Will the move operations be generated?

class User {
public:
    virtual ~User() = default;
    User( User&& )= default;
    User& operator=( User&& )= default;
    User( User const& )= default;
    User& operator=( User const& )= default;
};

Answer

In your first block of code you just declare the functions. The compiler is not going to automatically add a definition for those declarations. It is your responsibility to define the functions you declare.

Now, as a convenience = default was added so that if your class can use just the default implementation, but wouldn't because you added something to make it not do so automatically, then you can tell the compiler to do that for you explicitly.

This is handy when like in your example you want a virtual destructor, but all of the rest of the special member functions can have the default implementation.

Enjoyed this article?

Check out more content on our blog or follow us on social media.

Browse more articles