Boost logo

Boost :

From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2003-09-10 11:05:05


Hamish Mackenzie <hamish_at_[hidden]> wrote in message
news:1063192417.3100.25.camel_at_lightning.firestream.co.uk...
> On Tue, 2003-09-09 at 22:43, Fernando Cacciola wrote:
> > Hi boosters,
> >
> > Following the suggestions that had been made since Optional's acceptance
> > I've updated the library.
> > I will upload the new version on main CVS tomorrow noon unless
> > someone objects.
>
> Cool.

The new Optional is now on CVS.

The test files changed: many were added and two were deleted,
though the Jamfile now reflects this.
I've tested the new version with BCB6 (manually)
and gcc3.2 (via bjam)

>
> Does anyone else prefer value to get?

Not me :-)

>I think it will improve the
> readability of code using optional< shared_ptr< T > >.
>
I can't imagine code using optional< some form of pointer to T >
explicitely.
I can imagine that for generic programming, with optional<T>,
T itself might be shared_ptr<U> or some such, but in that case
.get() shouldn't be a problem.

> Any chance of adding .empty() member? I am not to fussed about this as
> it might make code that uses optional< bool > or optional< T * > clearer
> but will probably also make optional< std::vector< T > > more confusing.

The new optional<> now has ".is_initialized()"
for those who won't/can't use safe_bool or operator!

>
> Could you consider adding functions like these
>
> template< typename T >
> inline bool optional_empty( const T & a )
> { return false; }
>
> template< typename T >
> inline bool optional_empty( const optional< T > & a )
> { return a.empty(); }
>
> template< typename T >
> inline const T & optional_value( const T & a )
> { return a; }
>
> template< typename T >
> inline const T & optional_value( const optional< T > & a )
> { return a.value(); }
>
I prefer to leave this to end users as they can always do it using
the public interface.

Currently, optional<>'s inteface is not minimal as it should,
because it isn't clear yet which is the right interface.
Some duplicated functionaliy is there so that users can use
the subset they feel more comfortable with and I can gain feedback.
Eventually, in the future, I might shrink the interface to
its minimum once the right set of functions has been generally
stablished.

> to allow users to write code like this
>
> template< typename T >
> void f( const T & a )
> {
> if( !optional_empty( a ) )
> {
> std::cout << "a = " << optional_get( a );
> }
> }
>
> ...
>
> f( 3 );
> optional< int > x;
> f( x );
> x = 3;
> f( x );
>
> The advantage of this over
>
> template< typename T >
> void f( const optional< T > & a )
>
> is that allows that it avoids unnecessary copying of T .
>
I see your point but I'm not sure if this is a recommended pattern.
The purpose of allowing some degree of similarity between T and
optional<T> is to easy the _manual_ transition from one to the
other, but optional<T> is not intended to blindly replace T,
as in your example.

Fernando Cacciola


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