You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I recently had an issue understanding what is expected from types and standard library algorithms when it comes to self-move and self-swap and arrived at the following conclusion:
using std::swap; swap(x, x); is required to leave x unchanged as per the Swappable concept. Therefore standard algorithms are allowed to perform unguarded self-swaps.
The standard makes no guarantee concerning x = std::move(x) and from what I gathered around the internet some committee members expect it to leave x in a valid but unspecified state, so standard library algorithms have to avoid performing self-moves if they don't want to accidentally destroy elements (some algorithms expect non-overlapping input and output ranges, so they at least don't have to check for that).
Accordingly I wrote a type which detects self-moves and allows self-swap to try to detect such issues in one of my libraries. Looking at libwaful again, it might also be a suitable type for the library. Here is the current implementation if you're interested, but it currently uses exceptions instead of asserts to report errors, and has some overlap with some of libawful existing types (notably tracked). Nonetheless, I believe that the self-move/self-swap guarantees enforcement might be a valuable addition to the library :)
The text was updated successfully, but these errors were encountered:
I recently had an issue understanding what is expected from types and standard library algorithms when it comes to self-move and self-swap and arrived at the following conclusion:
using std::swap; swap(x, x);
is required to leavex
unchanged as per theSwappable
concept. Therefore standard algorithms are allowed to perform unguarded self-swaps.x = std::move(x)
and from what I gathered around the internet some committee members expect it to leavex
in a valid but unspecified state, so standard library algorithms have to avoid performing self-moves if they don't want to accidentally destroy elements (some algorithms expect non-overlapping input and output ranges, so they at least don't have to check for that).Accordingly I wrote a type which detects self-moves and allows self-swap to try to detect such issues in one of my libraries. Looking at libwaful again, it might also be a suitable type for the library. Here is the current implementation if you're interested, but it currently uses exceptions instead of asserts to report errors, and has some overlap with some of libawful existing types (notably
tracked
). Nonetheless, I believe that the self-move/self-swap guarantees enforcement might be a valuable addition to the library :)The text was updated successfully, but these errors were encountered: