Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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

	CD3dShaderPack(const CD3dShaderPack& other) = delete;				// copy constructor
//	CD3dShaderPack(CD3dShaderPack&& other) = delete;					// move constructor

//	CD3dShaderPack& operator=(const CD3dShaderPack& other) = delete;	// copy assignment
//	CD3dShaderPack& operator=(CD3dShaderPack&& other) = delete;			// move assignment

// TEST CASES

	Crealysm::D3DRENDERER::CD3dShaderPack firstPack;
	Crealysm::D3DRENDERER::CD3dShaderPack nextPack(firstPack);				// copy constructor

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

	Crealysm::D3DRENDERER::CD3dShaderPack thirdPack(std::move(nextPack));	// move constructor


Edited by cozzie

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this