Boost logo

Boost :

Subject: Re: [boost] [variant2] Formal review
From: Rainer Deyke (rainerd_at_[hidden])
Date: 2019-04-15 09:50:07


On 15.04.19 09:06, Emil Dotchevski via Boost wrote:
> On Sun, Apr 14, 2019 at 11:57 PM Rainer Deyke via Boost <
> boost_at_[hidden]> wrote:
>> No. A function is not required to check its invariants and
>> preconditions. If a function is defined as taking a non-empty variant,
>> then it is up to the caller to make sure the variant is not empty before
>> passing it to the function.
>
> The point is, there will be checks, in various functions (e.g. in "the
> caller"),

No checks are necessary, unless a variants passes from a context where
an empty state is allowed to one where it isn't.

If the empty state can only be entered via exception, then the only
context in which an empty state can exist is in the aftermath of an
exception, where we would have to either replace the empty variant with
a non-empty variant or allow the variant to leave the scope. But we
would have to do this even with a never-empty variant in order to
maintain our invariants, so the actual code would be no different in
either case.

> except if we know the state is impossible, A.K.A. the never-empty
> guarantee.

We can know that the state is impossible even if the guarantee is not an
intrinsic property of the variant type.

variant<int, bool> v = 1;
do_something_with(v);

Here 'do_something_with' is called with an argument that is never empty
- and never a boolean, and never an integer other than 1.

-- 
Rainer Deyke (rainerd_at_[hidden])

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