std::move

Why I cannot move native types with std :: move?

Ex.
1
2
3
4
5
int a = 5;
int i = std::move(a); /*/ a retains the value 5 /*/

std::string b = "hello";
std::string s = std::move(b); /*/ b dont contains "hello" /*/
You can, but it does not have much sense. Variable a was moved, but moving it does not have any difference from copying for built-in types.

Moved from variable remains in unspecified state which is guaranteed to destruct itself (by delete or automatic destructor call due to stack unwinding etc.) correctly. Using it in other ways aside from assigning new value to it is undefined behavior. Actual content is unknown. Actually if std::string was reference-counting, old string would be able to retain old value.

Point: do not assume anything about moved-from variables aside that they will not give you any problems if you just let them be.
Last edited on
POD (Plain-Old-Data) i.e. int, double, boolean, struct, float, etc... are not moved, they are copied.

https://stackoverflow.com/questions/11589028/efficiently-moving-a-class-with-pods
Smac89 wrote:
moved, they are copied.
They are moved. It is just they behavior in case of move and copy is the same. That because it is impossible to create more effective way of ownership transfer than simple copy for them.
Last edited on
Topic archived. No new replies allowed.