C ++ 11 - Move each element of an array (raw array, std :: array, std :: vector) separately?

In C ++ 11, all the movement of semantics, etc., one may wonder what can be really moved. Arrays are an example of this. Is it possible to move every element of raw arrays,

int array1[8]; int array2[8]; array1[0] = std::move(array2[0]); 

STD :: arrays,

 std::array<int, 8> array1; std::array<int, 8> array2; array1[0] = std::move(array2[0]); 

and std :: vectors

 std::vector<int> array1; std::vector<int> array2; array1[0] = std::move(array2[0]); 

separately?

+4
source share
1 answer

Of course, assuming that array1 and array2 correctly initialized with some data in your examples. When you deal with individual elements of an array in the order you describe, this is exactly the same process as when moving individual variables.

 Foo var1; Foo var2; var1 = std::move(var2); 

Here's a live example of your three pieces of code in action. .

Obviously, the "remaining" in the original variable after the move depends on the type of variable, but for now you do not need to read anything from the original variable, then you are fine.

+3
source

All Articles