0

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;
};
6
  • 8
    The first one says "I'm going to define them"; the second one says "I want you to define them". Commented Apr 15 at 19:41
  • 3
    "A relatively same question is asked before" - it would help if you link to that question so we can see if this is truly a new question and not a duplicate. Commented Apr 15 at 19:48
  • "but the code snippet is different" -- In what way? (Don't expect everyone to see the same thing in your code that you do. You should describe the difference in words.) Commented Apr 15 at 21:18
  • Both snippets declare the constructors and assignment operators. The second also directs the compiler to also define them (with a default implementation), which the first does not do. A program that uses those constructors or assignment operators will cause the difference to be exhibited - typically (assuming no other errors) a program using the second example will compile AND link (i.e. an executable is created) whereas the second will compile but not link (due to lack of definition of the constructors or assignment operators). Commented Apr 15 at 22:37
  • @JaMiT No I don’t. I wrote that sentence to say my question is not a repetitive one, in case if you saw something like that before. That's all. You can search and see what is the difference. Commented Apr 16 at 7:42

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.