# Understanding the move instructor (c++11)

This topic is 406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all,

I'm trying to understand how the new (C++11) move constructor and move assignment works in classes.

Initially I thought I could test by simply having a 1 on 1 relation between a specific creation/ assignment of the class, which would call one of the 4 member functions:

- copy constructor

- move constructor

- copy assignment

- move assignment

The code/ test below works, but for example when I comment out just the "copy constructor", both nextPack(firstPack) and std::move(nextPack) are no longer allowed (deleted). I would think that the 2nd one (std::move(nextPack)) would be allowed, because I didn't explicitly call delete/ specify the move constructor. Other combinations (just specify 1 of the 4 in the class definition, leads to 2 of the 4 situations not being allowed, instead of 1).

I hope someone can shed some light on this.

// CLASS DEFINITION

// TEST CASES

nextPack = firstPack;													// copy assignment
nextPack = std::move(firstPack);										// move assignment


Edited by cozzie

##### Share on other sites

Many things cause the compiler to implicitly delete constructors; the move constructor is particularly sensitive to implicit deletion. See here, the section on implicit deletion.

My suggestion to you in chat to explicitly delete them is because I feel that it's better, at this point in your understanding of the subject, to be explicit about it.

##### Share on other sites

Thanks, perhaps when I get more experienced on them, I'll be able to 'update' the code and remove 1 or 2 of the 4 that are now explicit.

For understanding the, current solution works fine, because I now know 100% sure that none of the 4 options will accidentally work.

I've read the URL/ link above, when I get the hang of it (or check all conditions) I could decide for each class:

- do nothing, move is fine (implicitly, no static members etc.)

- explicitly create the move constructor/ assignment myself

- explicitly define as 'deleted' (like I did with the ShaderPack class)

The same goes for copy constructor/ assignment I guess.

##### Share on other sites

Pete Isensee gave a great talk on this topic:

http://www.tantalon.com/pete/files/gdc12_faster_cpp.zip

##### Share on other sites

Thanks, perhaps when I get more experienced on them, I'll be able to 'update' the code and remove 1 or 2 of the 4 that are now explicit.

I'm not aware of any reason to prefer implicit deletion over explicit.

Relying excessively on implicit behaviour tends to be a sign of someone trying to write "clever" code, and at some point, either your coworkers, or you when you try and read this code again in a year, will be glad that you made it explicit.

##### Share on other sites

This topic is 406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628730
• Total Posts
2984431

• 25
• 11
• 10
• 16
• 14