Advertisement
Barteks2x

Untitled

Jul 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. [20:55:37] <barteks2x> Does remove_if call destructor of the objects being removed?
  2. [20:55:50] <barteks2x> (I mean std::remove_if)
  3. [20:58:18] <RandomReader> no, it only moves things, hence the erase-remove idiom
  4. [20:58:22] <RandomReader> https://en.cppreference.com/w/cpp/algorithm/remove
  5. [20:59:14] <barteks2x> but here http://www.cplusplus.com/reference/algorithm/remove_if/ it also says that the objects are left in valid *but unspecified* state
  6. [20:59:27] <barteks2x> which means, anything nontrivial can't be safely destructed
  7. [21:00:02] <davidstone> A valid state requires destructibility
  8. [21:00:23] <davidstone> A destructor cannot have preconditions
  9. [21:00:33] <barteks2x> but if it's not the same state that it was before remove_if was called, then it's not the same thing being destructed
  10. [21:00:47] <barteks2x> (for example what if destructor needs to free some external resources?)
  11. [21:01:10] <davidstone> The move assignment operator should do that, or else leave it in a state such that the destructor does it.
  12. [21:02:12] <davidstone> Consider std::vector: the move assignment operator will typically leave the source of the move assignment with nothing that it owns, release any memory previously owned by the target, and the memory that used to be owned by the source is now owned by the target. The destructor on the source is now a no-op.
  13. [21:02:28] <davidstone> In some cases involving allocators, the move assignment operator is a copy instead. Then the destructor of the source will free the memory
  14. [21:03:43] <barteks2x> And how does remove_if change state of the objects?
  15. [21:04:05] <davidstone> Through the move assignment operator
  16. [21:04:16] <davidstone> The objects at the end of the range are left in the moved-from state
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement