Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2003-08-31 07:09:24


Joel de Guzman wrote:
> Although I don't see this as problematic:
>
> optional<int> x;
> if (x)
> foo(x);
>
> Or perhaps:
>
> optional<int> x;
> if (!!x)
> foo(x);
>
> We already have an implicit conversion to safe_bool and an
> operator ! anyway. Keep it. There's nothing wrong with it:
>
> operator unspecified-bool-type() const;
> bool operator!() const;

IMHO, there is something terribly wrong here because now optional<T> has
two interfaces. The interface of optional itself and the interface of T.
If you think that optional<T> can be used like T (having the
value-interface), you are immediately fooled by if(x) as it doesn't
check T's value. A pointer-interface is much cleaner as it gives the
user a hint that he is using a wrapper and in practice, I always prefer
to be a little more explicit on these things (even at the cost of an
occasional * here and there) than to have silent bugs.

Regards, Daniel

-- 
Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de

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