r/cpp • wx | soci | swig • Jun 17 '15

Standardizing Variant: Difficult Decisions

https://www.justsoftwaresolutions.co.uk/cplusplus/standardizing-variant.html
31 Upvotes

56 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Jun 18 '15 edited Jun 18 '15

For move assignment, A = std::move(B)... to preserve iterators you would need to move B's sentinel over to A and B would need to allocate a new sentinel to represent its new end. That allocation could potentially throw, and hence move assignment can not be noexcept.

I read the ISO C++ mailing list and remember reading about various ways to solve this problem, and it's unbelievably ugly, not to plug Rust or anything, but they got this right. In Rust, move operations never throw, for any object, period. Moves in Rust just work as you would expect them to and I think C++ could potentially learn from that language by making move semantics destroy the original object, as in make the object unusable after a move operation.

If the object becomes unusable after a move... this problem goes away, along with a host of other issues.

4

u/Plorkyeran Jun 18 '15

For move assignment, A = std::move(B)... to preserve iterators you would need to move B's sentinel over to A and B would need to allocate a new sentinel to represent its new end.

Why can't you just swap the sentinels?

2

u/whichton Jun 18 '15

There was a proposal for destructive move, but it got rejected I believe. Destructive move is very useful for things like sorting a container.

Problem is C++ constructor does 2 very different things - it allocates memory, whether on stack or heap and then it constructs the object on the heap. C++ has no way to express the "has memory but not yet constructed" state. Hence we often find the default constructed object being used as a proxy, which is incorrect. Stepanov has argued for such a state is his papers, and I believe the destructive move proposal was in response to his ideas