4

In C++11 with all it's move semantics and such, one may wonder what can actually be moved. An example of this are arrays. Is it possible to move each 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]);

individually?

1 Answer 1

3

Of course, assuming that array1 and array2 is properly initialized with some data in your examples. When you're dealing with individual array elements in the manner you describe, it's exactly the same process as when moving single variables.

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

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

Obviously, what's "left over" in the source variable after moving depends on the variable's type, but as long as you don't need to read anything from the source variable, then you're fine.

Sign up to request clarification or add additional context in comments.

8 Comments

So, whatever happens to the source variable, the one, of which a value will be moved? Will it be free storage for application(s) to use in the system? Is this undefined?
@Helixirr: No, it's not free storage -- it's only destroyed in a logical sense. It still needs to have its destructor called in order to turn back into raw storage, because it might have internal management data that wasn't moved.
@Helixirr: Informally speaking, it will be in a state where you can't guarantee that any of the original data still exists in it, but you can still destruct it safely. For example, if we're moving std::strings, the source string will be empty (since all that's happened is copying only the pointers instead of the entire array). But you can still destruct the source variable (by letting it go out of scope).
@Mehrdad: Aah, basically, the value a programmer uses will be moved, but internal data the system itself is interested in remains the same until it's destroyed via destructor? Is this why source pointers in the move constructors have to be assigned to point to nullptr?
@Helixirr: Yes, exactly. As for making them point to nullptr, it's because it may be useful for the internal implementation (for example, the implementation of std::vector may set its internal pointer to null when the data is moved out, so that if you were to subsequently move-assign something into it again, it would know that it doesn't need to free existing data). If it isn't useful then you don't have to do it, it just depends on what the implementation needs in order to do its job. From the user's perspective, you can only either destroy a moved-from object, or move something else into it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.