copy/move constructor and std::vector?

Started by
1 comment, last by cozzie 7 years, 5 months ago

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?

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

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

What happens if the vector's storage is full when you emplace_back? The vector would have to re-allocate it's internal storage and move / copy the elements in the old storage to the new one.

Thus, your elements need to be moveable / copyable.

Alternatives include containers that don't reallocate, or storing (smart) pointers, or implementing the required functionality. Copying can be tricky (e.g. copying textures) but moving is, I believe, not too bad to implement.

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.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement