Timeline for C++ Vector The basics
Current License: CC BY-SA 3.0
5 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Mar 23, 2016 at 23:06 | comment | added | Loki Astari |
Yes. After you have moved an object what is left behind is unspeciied as long as the object is valid. But you can still re-use it if you set it to a known state before use. std::string a = "X";std::string b = "Y";a = std::move(b); /* b can still be used. But you need to set it to a known state before you can do anything useful */ b.clear(); . If I had deallocate the memory associated with b then you would have had lots of unnessacery work in resource management. But simply setting its state to empty it becomes re-usable with no memory allocation required.
|
|
Mar 23, 2016 at 22:55 | comment | added | ilmale | I think you are right, but the expected behaviour is that a moved object is not the owner of the resource. From cppreference "Move assignment operators typically "steal" the resources held by the argument (e.g. pointers to dynamically-allocated objects, file descriptors, TCP sockets, I/O streams, running threads, etc.), rather than make copies of them, and leave the argument in some valid but otherwise indeterminate state. For example, move-assigning from a std::string or from a std::vector may result in the argument being left empty. However, this behaviour should not be relied upon." | |
Mar 23, 2016 at 22:09 | comment | added | Loki Astari | Thanks for the review. All input gratefully received. There is no need to invalidate the moved object though (after a move the object just needs to be valid). And you should definitely not deallocate the memory. The move objects destructor will handle this until then it could be potentially re-used. | |
Mar 23, 2016 at 21:42 | comment | added | ilmale | I notice now that they spotted the iterator bug in a comment of the original post.. >_> | |
Mar 23, 2016 at 21:40 | history | answered | ilmale | CC BY-SA 3.0 |