Boost logo

Boost :

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


On 06.04.19 02:46, Emil Dotchevski via Boost wrote:
> I do not think that it is a bug to access an object that is in a valid
> state. In fact, the reason why the state is defined as valid is so that you
> can safely work with that object, even after a failure was reported.

One thing that bothers me about this view is that it is inconsistent
with how the language treats uninitialized variables. Every bit pattern
of a variable of type 'char' is a valid value. However, reading from an
uninitialized variable of type 'char' is undefined behavior. On a
physical level, a variable of type char will always have a valid value,
but on the conceptual level, it can also have a special "uninitialized"
state from which it is legal to assign a new value but not to read the
current value.

I find that using this conceptual uninitialized state actually makes it
/easier/ to reason about the correctness of my code. Any code that
depends on the value of an uninitialized variable for its behavior is
automatically incorrect. Any code that reads an uninitialized variable,
even if the value read does not affect the observable behavior of the
code, is automatically incorrect. Just declaring an uninitialized
variable is a red warning light that I need to be careful to assign a
value to that variable before reading from it. So long as I follow
these rules, I never have to worry about my code unexpectedly breaking
due to an uninitialized variable having an unexpected value.

To me, the "valid but unspecified" state of an object after an exception
is thrown from a function with the basic exception guarantee is
conceptually very similar to the state of an uninitialized variable. If
I catch such an exception while the object is still in scope, that's a
red warning light that I need to either assign a new state to the object
or allow the object to go out of scope. Anything else risks the same
sort of errors as are caused by reading from an uninitialized variable.

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