0

Since std::move() is just a cast operation, the default move constructor do not really empty the parameter. This lead to that I have to write all the move constructor by myself, which is boring and it's easy to make mistakes(for example I add a member but forgot to deal it with in the move constructor).

So any compile flags to make my life easier?

What's more, some guys said that std::move() do not empty the value is because the "you do not pay for what you don't need" principle, I'm curious that is there any case that the "empty" operation is not needed? All destructors need to use the value to decide to release resources or not, "empty" the value seems a must for me...

  • Are you using types like `std::unique_ptr` or standard containers to manage resources? Or is this this about every possible type under the sun? – StoryTeller - Unslander Monica Jun 09 '20 at 08:12
  • @StoryTeller-UnslanderMonica, I'm dealing with opengl, so all the type are integers. –  Jun 09 '20 at 08:14
  • I don't think there is any semantics of _empty_ data in C++. What is an empty integer or empty float? Zero? Why zero? What is an empty bool? All subobjects basically boil down to some fundamental types. The only case where empty semantics might be reasonable are pointers, but it also does not make sense. For instance, with SSO, the moved-from string object has a data poiner set to its buffer, therefore, it's not null. Moreover, with pointers, you can use unique-pointers and then you will get empty-after-move automatically. – Daniel Langr Jun 09 '20 at 08:32
  • @DanielLangr may I ask what SSO represent for? –  Jun 09 '20 at 08:36
  • @ravenisadesk Short string optimization, where string objects contain some buffer to avoid dynamic memory allocations for short strings. – Daniel Langr Jun 09 '20 at 08:37
  • @DanielLangr, thanks, I'll investigate. –  Jun 09 '20 at 08:38
  • @ravenisadesk Anyway, if you really insist on this functionality for types such as integers, you can create a simple wrapper around the object of that type (which shouldn't add any overhead), define it's move constructor with your emptiness-semantics, and use it practically transparently in your class instead of the original integer member variables. – Daniel Langr Jun 09 '20 at 08:41
  • @DanielLangr, sounds acceptable... I'll try. –  Jun 09 '20 at 08:57
  • There is no "empty" operation required to move an object. For a description of how move semantics works, have a look at https://stackoverflow.com/questions/3106110/what-is-move-semantics – Peter Jun 09 '20 at 08:57

0 Answers0