Boost logo

Boost :

Subject: [boost] Strong guarantee variant (Was: Outcome/expected/etc/etc/etc)
From: Peter Dimov (lists_at_[hidden])
Date: 2017-06-06 10:37:28


Niall Douglas wrote:

> I hate with a passion any possibility of a variant changing its state
> without me explicitly telling it to do so. The only tolerable state a
> variant should ever choose on its own is empty/valueless.

I wonder whether the people who insist on the strong guarantee on assignment
realize that when the variant holds a vector, and you assign it a vector,
assignment delegates to vector<>::operator= and as a result you get the
basic guarantee because that's what all standard types provide.

That is, the "variant state" - by which you probably mean the index -
doesn't change but the variant state can change into holding a vector that
is neither the old nor the new one.

It's possible to define a variant that never delegates to the assignment
operator of the contained type. I've also argued for this possibility,
because it makes it possible for one to put non-assignable types into
variant (and optional, if it's changed to match), and still get an
assignable variant.

Nevertheless, this is not the case today, and I suspect that you don't
realize that the strong guarantee requires this change.

> 1. If at least all types minus one have nothrow move construction, you get
> the strongest possible never empty never change guarantee.

What do you do on emplace<throwing_move>(), when the variant already
contains 'throwing_move'?

And what do you do on operator=( throwing_move ), when the variant already
contains 'throwing_move' and throwing_move::operator= doesn't provide the
strong guarantee?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk