Boost logo

Boost :

Subject: Re: [boost] [optional] generates unnessesary code for trivial types
From: Domagoj Saric (domagoj.saric_at_[hidden])
Date: 2012-02-02 10:20:27


On 30.1.2012. 19:58, Dave Abrahams wrote:
> I support this work. Optional should be optimal :-)

Or, optimal should not be optional (everything should be optimal :D).

>> a) the lifetime management bool was changed into a properly typed pointer (this
>> actually takes the same amount of space while it provides a no-op get_ptr()
>> member function as well as easier debugging as the contents of optional can
>> now clearly be seen through the pointer, as opposed to gibberish in an opaque
>> storage array)
>
> Seems to me this potentially makes optional<char> much bigger. No?

True (see my answer to Olaf and Christofer).

>> h) optional marks itself as uninitialised _before_ calling the contained
>> object's destructor (this makes it a little more robust in race conditions;
>
> I generally disagree with this sort of defensive programming. Won't it just
> mask bugs?

I generally disagree too but in cases where there is actual "defensive
programming" i.e. handling of invalid/buggy usage. The typical example is code
that asserts that a pointer is not null and then handles the case if it is.
There is none of that here. Imaging writing optional from scratch, at one point
you would have to decide the same thing, when to mark the optional as empty -
before or after calling the destructor. Either way you choose won't make a
difference (semantic or performance wise) for correct code. Incorrect code will
crash less. Isn't that a good thing (considering there is no actual handling of
incorrect code)?
Considering that "there is no bug free software" (one wonders about laser brain
surgery robots :), wouldn't it be better to "a priori crash less" and add
separate sanity checks for invalid concurrent access in order to catch bugs
(obviously this is more work and I don't know if any Boost component does
anything like this actually)?

Perhaps there is no "right" answer to this question and its more a matter of
preference so consider the above as "my 2 cents"...

ps. and yes, I forgot the buzz: (p) rvalue references support :)

-- 
"What Huxley teaches is that in the age of advanced technology, spiritual
devastation is more likely to come from an enemy with a smiling face than
from one whose countenance exudes suspicion and hate."
Neil Postman

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