Boost logo

Boost :

Subject: Re: [boost] [variant2] Andrzej's review -- design
From: Rainer Deyke (rainerd_at_[hidden])
Date: 2019-04-08 06:47:59


On 07.04.19 21:40, Emil Dotchevski via Boost wrote:
> On Sun, Apr 7, 2019 at 1:38 AM Rainer Deyke via Boost <boost_at_[hidden]>
> wrote:
>> You keep using the term "valid" as if its a clear-cut binary
>> distinction.
>
> Do you see that this is a matter of definition?

No. You can use any terminology you want. I'm pointing out that the
distinction between "valid" and "invalid", by your definition, may not
be the most important distinction to consider.

> Stronger: to argue that either A or B is an invalid state is equivalent to
> arguing that the basic guarantee may leave the program in an invalid state.

I would rather say that the basic guarantee is the minimum guarantee
that allows the program to maintain a valid state. It is up to the
caller of a function with the basic guarantee to ensure that any broken
higher-level invariants are restored.

>> However, I can think of at least four different degrees of
>> validity:
>>
>> 1: Garbage. A variable (of a class type) was not properly constructed
>> and contains complete garbage. It is undefined behavior to perform any
>> operation on the object, including assignment and destruction.
>>
>> 2: Uninitialized. A variable (of a built-in type) is uninitialized. It
>> is undefined behavior to read the value of this variable, but the
>> variable is "valid" in the sense that you can assign a new value to it
>> and that you can destruct it.
>>
>> 3: Indeterminate. A variable (of a class type) has an indeterminate,
>> semantically meaningless state (after throwing an exception from a
>> member function with the basic exception guarantee, or after being
>> pulled from an object pool). It is technically allowed by not
>> semantically meaningful to read the value of this variable, but the
>> variable is "valid" in the sense that you can assign a new value to it
>> and that you can destruct it.
>>
>> 4: Correct. A variable (of any type) is valid and contains a
>> semantically meaningful and correct value.
>>
>>
>> You seem to categorize degrees 3 and 4 as "valid" and degrees 1 and 2 as
>> "invalid".
>
> I'm using existing, well established definitions. C++ is not defined in
> terms of the above degrees of validity, and it does not make a distinction
> between 3 and 4.

But when writing a program that actually has a function beyond avoiding
undefined behavior, the distinction between a variable that contains a
correct value and a (valid, by your definition) variable that contains
garbage is critical. Using a garbage value for further computation is
defined but clearly incorrect behavior.

-- 
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