2017-05-30 14:25 GMT+02:00 Peter Dimov via Boost
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.
I like that. Regards, &rzej;