Sign in to follow this  

copy/move constructor and std::vector?

This topic is 401 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,

While developming my engine/ framework I've encountered the following situation:

 

- let's say I have class A: mySomethingClass and class B: mySomethingClassMgr

- class B has a member std::vector<mySomethingClass>

- I use the std::vector functions to use, fill and manage the std::vector in class B

 

But when I make the copy/ move constructors/assignment 'deleted'/ not allowed in class A, the std::vector functions don't work anymore, including "emplace_back".

//	CD3dShaderPack(const CD3dShaderPack& other) = delete;				// copy constructor: not allowed
//	CD3dShaderPack(CD3dShaderPack&& other) = delete;					// move constructor: not allowed
//	CD3dShaderPack& operator=(const CD3dShaderPack& other) = delete;	// copy assignment: not allowed
//	CD3dShaderPack& operator=(CD3dShaderPack&& other) = delete;			// move assignment: not allowed

mShaderPacks.emplace_back();
private:
	std::vector<CD3dShaderPack>			mShaderPacks;

My conclusion from this, is that "emplace_back" is somehow using a copy or move constructor/assignment.

The error I get when I not comment out the 4 lines above, is:

error C2280: 'Crealysm::D3DRENDERER::CD3dShaderPack::CD3dShaderPack(Crealysm::D3DRENDERER::CD3dShaderPack &&)': attempting to reference a deleted function
1>  e:\projects\crealysm11\crealysm11\d3drenderer\cd3dshaderpack.h(32): note: see declaration of 'Crealysm::D3DRENDERER::CD3dShaderPack::CD3dShaderPack'
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,_Ty>(_Objty *,_Ty &&)' being compiled
1>          with
1>          [
1>              _Ty=Crealysm::D3DRENDERER::CD3dShaderPack,
1>              _Objty=Crealysm::D3DRENDERER::CD3dShaderPack
1>          ]

When I comment out the 1st, 2nd and 4th and keep the 'copy assignment' = delete in place, it also works.

So it's a combination of the other 3 that gives the problem (tried all combinations, all 'NOK').

 

Do you know if there's a way to use the std::vector in class B with class A objects, without allowing copy/move constructor/assignment for class A?

Edited by cozzie

Share this post


Link to post
Share on other sites

Thanks, sounds clear.

In this case I find it not worth it to write my own array/vector container (instead of using std::vector), especially because I switched to smart pointers for the 'tricky' variables that led me to not allowing copying/moving initially. In the future I might be using a std::vector with smart pointers to the class 'A' objects. I'm also learning, so trying to take it step by step.

Share this post


Link to post
Share on other sites

This topic is 401 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