Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2001-09-14 09:30:49


----- Original Message -----
From: Peter Dimov <pdimov_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, September 14, 2001 9:16 AM
Subject: Re: [boost] More on the optional class.

> From: "Fernando Cacciola" <fcacciola_at_[hidden]>
> [...]
> > 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 apologize if this has already been discussed, but did you consider the
> idiom (often used with dynamic_cast)
>
> optional<T> opt;
>
> if(T * pt = opt.get())
> {
> // initialized, use pt
> }
> else
> {
> // not initialized, no pt in scope
> }
>
This is already supported.
I've added .get() precisely in order to support this idiom, but I've been
kind of reluctant to talk about it.
It usage is *barely* mentioned in the documentation included in the header.
I'm not sure if I would encourage this idiom since it uses a member
function, and I'm not very fond of using member functions for general
classes -which could be replaced for something similar-
In fact, optional includes member function .initialized(), but I don't
encourage its usage for the same reasons as above.

Anyway, your sample code is entirely valid -including the usage of the bare
pointer inside the block- and can be used by any user who like it.
I realize that the 'get' member function, unlike other member functions, is
pretty common and an 'idiom' in itself, so perhaps it is not a bad idea to
be more explicit about its existence. I'll think about adding the sample
code above in the documentation and test.

Cheers,

Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com

> --
> Peter Dimov
> Multi Media Ltd.
>
>
> Info: http://www.boost.org Unsubscribe:
<mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>


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