|
Boost : |
Subject: Re: [boost] [outcome] High level summary of review feedback accepted so far
From: Peter Dimov (lists_at_[hidden])
Date: 2017-05-30 12:25:50
Niall Douglas wrote:
> > value() id wide operator*() is narrow.
>
> Why? Just because optional made that mistake?
The motivation here is that in
if( o )
{
o->this_();
o->that();
o->this_other_thing();
o->that_too();
o->almost_forgot();
}
there is needless replication of the check. This, as I already mentioned,
stems from a desire to operate on the value inside 'o' directly, which is
not my preferred style in result's case, where I'd prefer to extract it
instead.
There is a middle ground here, which I was pondering for a while, make
has_value return T* instead of bool.
T* has_value();
T const* has_value() const;
Returns: a pointer to the contained value if one is present, otherwise
nullptr
Now the above sequence is spelled
if( auto* p = o.has_value() )
{
p->this_();
p->and_so_on();
}
Not sure I particularly like that, but for people who like "consistency with
X" arguments, this is like variant's get_if, whereas value() is like
variant's get().
This still allows the ordinary if( r.has_value() ) check to work as-is, so
nothing is lost in principle.
But if this has_value signature strikes you as a bit too novel, the more
traditional T* value_if() is also an option. Note how it strikes a balance
where the function itself has a wide contract, but then if you use the
returned pointer without a check you're on your own.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk