Smart pointers and hidden implementation

I'm trying to make a private implementation for a vertex class. I am using smart pointers to store its values, but for some reason I can't transfer ownership with std::move.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Vertex::VertexImpl //hidden implementation
{
public:
	VertexImpl(const float & X, const float & Y, const float & Z);
	~VertexImpl();
	VertexImpl(const Vertex::VertexImpl & other);
	VertexImpl(const Vertex::VertexImpl && other);

	Vertex::VertexImpl & operator=(const Vertex::VertexImpl & other);
	Vertex::VertexImpl & operator=(const Vertex::VertexImpl && other);

private:

	std::unique_ptr<GLfloat> yaw;   //z
	std::unique_ptr<GLfloat> pitch; //y
	std::unique_ptr<GLfloat> roll;  //x
};


1
2
3
4
5
6
Vertex::VertexImpl::VertexImpl(const Vertex::VertexImpl && other)
{
	yaw  =std::move(other.yaw);
	pitch=std::move(other.pitch);
	roll =std::move(other.roll);
}


This is the error I get


Error (active)		function "std::unique_ptr<_Ty, _Dx>::operator=(const std::unique_ptr<_Ty, _Dx>::_Myt &) [with _Ty=GLfloat, _Dx=std::default_delete<GLfloat>] cannot be referenced -- it is a deleted function


Thanks in advance.
> I am using smart pointers to store its values
¿why?


About your error, `other' is constant.
Also, you may want to use the initialization list there.
About your error, `other' is constant.


Damn, I feel stupid :P Thanks for the heads up

I am using smart pointers to store its values

Clean-up purposes
Clean-up purposes

No clean up required if you don't use pointers.
The better way to write that constructor is to omit it or default it (see http://en.cppreference.com/w/cpp/language/rule_of_three#Rule_of_zero )
VertexImpl(Vertex::VertexImpl&&) = default;

Also, if that's how you're writing constructors, you're double-initializing your classes. By the time the opening brace of a C++ constructor is reached, every member and base is fully initialized, and undoing all that only to replace the members again as you did with yaw =std::move(other.yaw); etc is doing fruitless work:
1
2
3
4
5
6
Vertex::VertexImpl::VertexImpl(Vertex::VertexImpl&& other)
: yaw(std::move(other.yaw)),
  pitch(std::move(other.pitch)),
  roll(std::move(other.roll))
{
}

Last edited on
Topic archived. No new replies allowed.