A Question Regarding std::move()

Jun 27, 2012 at 7:43pm
closed account (zb0S216C)
If I construct an object at location A (with placement new) and attempt to move that object to location B with std::move(), will I have to call the destructor of the object at location A? Or will the compiler do that for me?

Wazzak
Last edited on Jun 27, 2012 at 7:46pm
Jun 27, 2012 at 7:55pm
Do you /need/ to call the destructor though? If the resources of the object have already been moved elsewhere (and are now owned elsewhere), there shouldn't be anything that needs to be cleaned up, if my understanding is correct.

Edit: maybe I can see some situations where you would still need the destructor. I think you would have to call it explicitly.
Last edited on Jun 27, 2012 at 8:46pm
Jun 27, 2012 at 8:08pm
Does this help?
http://msdn.microsoft.com/en-us/library/dd293665.aspx
I haven't used std::move at all yet, but this article looks related.
Jun 27, 2012 at 9:55pm
You still have to call the destructor.
Jun 27, 2012 at 10:23pm
Moving leaves the moved-from object in valid state, the compiler does not call the destructor for you.
Jun 28, 2012 at 8:44pm
closed account (zb0S216C)
Thanks for the replies :)

@Peter87 & Cubbi:
It's hardly moving then is it? It turns out that std::move() just prepares its argument for moving whilst avoiding the copy-constructor; it's the move-constructor that does the moving - kinda misleading, really.

Any road, at least I know for the future.

Oh, and for anyone who reads this in the future: GCC supports implicit move-constructors, while Microsoft's Visual C++ does not; not even in Visual C++ 2011, apparently.

Thanks again!

Wazzak
Last edited on Jun 28, 2012 at 8:45pm
Jun 28, 2012 at 9:19pm
std::move() just prepares its argument for moving whilst avoiding the copy-constructor; it's the move-constructor that does the moving - kinda misleading, really.

Not necessarily constructor, it works the same way with assignment, push_back(), or any other function that has lvalue- and rvalue-taking overloads. And yes, std::move is a bit misleading, I'd call it std::xvalue
Topic archived. No new replies allowed.