|
Boost : |
From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2005-10-24 12:23:30
Simon Buchan wrote:
> Fernando Cacciola wrote:
>> Hamish Mackenzie wrote:
>>
>>> On Mon, 2005-10-17 at 16:09 -0300, Fernando Cacciola wrote:
>>>
>>>> But that's not because optional<> is a pointer, is becasue both
>>>> optional<> and pointer's are somehing else: OptionalPointees.
>>>
>>> Let me say first I don't use optional references (yet anyway) and I
>>> am
>>> quite happy with your explanation of the need to rebind on
>>> assignment.
>>>
>>
>> Good.
>>
>>
>>> I am, however, still not convinced that OptionalPointees is
>>> justification for using the interface of a pointer.
>>
>>
>> OK
>>
>>
>>> I still sometimes
>>> think of optional as a vector with a maximum size of one, but I now
>>> think it would be a bad idea to copy member names from std::vector,
>>> as
>>> it is likely to cause confusion when using optional< vector< T > >.
>>> For
>>> the same reason I think optional< bool > is very problematic with
>>> the current interface (see my earlier post in this thread).
>>
>>
>> The problems with optional<bool> are totally unrelated to the choice
>> of operators * and ->.
>> It is the safe_bool() operator _alone_ which causes that.
>> We can of course discuss whether such operator is worth it given the
>> ambiguity it creates with optional<bool>. That was discussed at
>> length at the time optional<> was reviewed (with me initially
>> opposing safe_bool()), in the end, we agreed that it had more
>> benefits than this simple counter-example.
>>
>>
>>> Could the unifying concept OptionalPointee be implemented as free
>>> functions?
>>
>>
>> Of course.
>>
>> Notice that there are alternatives for safe_bool() that doesn't
>> require dropping * and ->.
>> These alternatives fix the problem with optional<bool>.
>> One example is to simply provide operator !, used like this:
>>
>> if ( !!opt )
>> is_initialized();
>>
>> (this is what my initial boost submission did)
>>
> wouldn't requiring nil/none comparison make more sense?
More sense than what? safe_bool()?
I'm not sure...
Beign able to just write
if ( o )
or
if ( !o )
is too important to let go.
optional<> doesn't provide conversion to T so that expression can never
refer to the wrapped value, just like that expression on a pointer can never
refer to the pointed object.
> optional<bool> opt = ...;
> if(opt != none)
> { // is initialized
> if(opt)
> { // is true
> ...;
> }
> if(!opt)
> { // is false
> ...;
> }
> }
> if(opt == none)
> { // isn't initialized
> }
>
> Would the single conversion rule apply to:
> optional<char> oc = next_char();
> if(oc == none)
> // input failed!
> recover();
> if(oc) // optional<char> -> char -> bool ?
> // not the end of input
> print(oc);
>
No. optional<T> cannot be converted (not even explicitely) to T.
-- Fernando Cacciola SciSoft http://fcacciola.50webs.com/
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk