Boost logo

Boost :

From: Dave Gomboc (dave_at_[hidden])
Date: 2003-03-25 22:01:23


> It would be nice if boost::optional<T> had operator< defined whenever
> operator< was defined for T. This would allow us to use optional<T>
> as the key of an associative container. I suggest the following
> semantics:
>
> bool operator<(optional<T> const &x, optional<T> const &y);
>
> Returns: If y is uninitialized, false. If y is initialized and x is
> uninitialized, true. If x and y are both initialized, (*x < *y).
>
> This results in a strict weak ordering with uninitialized optional<T>
> objects being sorted first.

Yes, I previously implemented this functionality -- exactly in the manner
you described -- in my nilable<T> implementaton. It's quite useful to
have.

(If anyone is curious, nilable<T> was inspired by optional<T>, though the
implementation is not derived from optional's. The main differences are
that nilable<T> doesn't use pointer deferencing for access to the internal
value. Instead, it provides a direct, checked conversion to T (using
boost::throw_exception with a bad cast error when the object obj is nil),
obj.nil() returning bool, and obj.unsafe_reference() and
obj.unsafe_value() for when one has previously ensured obj.nil() is false
and want to make further accesses without that being tested repeatedly.)

Dave


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