r/cpp • u/_VZ_ wx | soci | swig • Jun 17 '15
Standardizing Variant: Difficult Decisions
https://www.justsoftwaresolutions.co.uk/cplusplus/standardizing-variant.html
30
Upvotes
r/cpp • u/_VZ_ wx | soci | swig • Jun 17 '15
View all comments
Show parent comment
4
u/[deleted] Jun 18 '15 edited Jun 18 '15
Those containers use so-called sentinel nodes to mark the end of the container. So
std::listhas a dummy/sentinel node to represent theend().These dummy nodes can be dynamically allocated, or they can be embedded within the container.
If they are dynamically allocated well then obviously even just constructing the container may throw an exception since that involves a dynamic allocation. The same goes for moving it as well since a move operation will have to allocate new sentinel nodes which could fail. So dynamically allocating the sentinel node can not be
nothrow.If they are embedded within the container then you avoid a dynamic heap allocation, which is generally a good thing... but the downside is that you end up with iterator invalidation when doing a
swap. Since the sentinel node is embedded inside of the container and is used to refer to theend()it means that after aswap, the iterator to theend()will be invalid.Now this is where there is contention. The standard says it's permissible for a
swapto invalidate an iterator to theend(), but it also implies that it's okay for it to not invalidate that iterator. It's basically up to the implementation to decide. An implementation can choose to make move constructors non-throwing, or makeswappreserve end iterators, but it can not do them both.