|
Boost : |
From: Daryle Walker (darylew_at_[hidden])
Date: 2001-09-14 19:26:10
on 9/13/01 8:18 PM, Fernando Cacciola at fcacciola_at_[hidden] wrote:
[SNIP]
> I also made up my mind about how to let the user test if a given optional is
> initialized or not.
> Previously, optional<> used operator void*() to allow expressions of the
> form:
>
> if ( opt ) or if ( opt != NULL )
>
> After *a lot* of consideration, I decided to support only the 'bang-bang'
> idiom:
>
> if ( !!opt ) means initialized.
> if ( !opt) means uninitialized.
>
> I just hope enough people is aware and fond of this very useful technique,
> because it removes all the problems with the other approaches.
> The new optional.hpp header contains a small rational for this design
> decision.
[TRUNCATE]
What about this alternative:
You had an "operator void*()" defined for Boolean tests, to avoid the
integral conversions that "bool" has. However, this property isn't just
limited to void pointers, it will work for any pointer type. Since your
class acts kind-of like a smart pointer, why don't you define the Boolean
operation with "operator T*()" instead! You'll get "operator !()" for free
and you could connect it with an "operator *()" and "operator ->()".
-- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT mac DOT com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk