Move constructor, move vector between two objects using std :: move?

I have an object similar to the following, and I'm trying to implement a move constructor, so you can have an insert for std::vector<Mesh> .

 struct Mesh { std::vector<Vector3> vPoint; bool Valid; Mesh(Mesh&& other) { vPoint = std::move(other.vPoint); Valid = std::move(other.Valid); } }; 

Is it correct? And if so, what is the value of other.Valid after std :: move works on it?

Edit:

Also, if I have an instance of this object, do I need to use std :: move in the following script?

 std::vector<Mesh> DoSomething() { Mesh mesh; //Imagine vPoint is filled here to std::vector<Mesh> meshes; meshes.push_back(std::move(mesh)); // Here is my question, std::move? or just pass mesh here? return meshes; } 
+8
c ++ c ++ 11 move-semantics
source share
2 answers

You should write your move constructor as follows:

 Mesh( Mesh&& other ) : vPoint( std::move( other.vPoint ) ) , Valid( std::move( other.Valid ) ) {} 

The disadvantage of assigning inside the constructor body, as opposed to using the constructor initializer list, is that in the first case, the member objects of the Mesh object that you move are constructed by default and then assigned inside the body. In the latter case, they are created directly from the result of calling std::move .

You should not read an object, whether it is an integral type or a more complex object, after moving it. Such objects exist in an unspecified state.

+9
source share

(partial answer is the answer to the bit about move ing bool )

cppreference.com has the following message about std::move :

The library code is necessary to leave a valid value in the argument, but if there are no documents of type or function otherwise, there are other restrictions on the resulting value of the argument. This means that it is generally wise not to use the hyphen again from the argument.

Thus, you cannot rely on bool to true or false after move on it.

+3
source share

All Articles