Boost logo

Boost :

From: Hamish Mackenzie (hamish_at_[hidden])
Date: 2003-09-09 14:23:30


On Tue, 2003-09-09 at 18:55, Pavel Vozenilek wrote:
> An example where this interface fits better than current one?

It fits better in my code, as I have been using a similar class for some
time (only I called it allow_null). So I am a little biased in that
respect.

I did make a conscious decision not to use pointer semantics in my
allow_null class. Making it look like a pointer gives me the impression
that it will shallow copy like a pointer. In fact it deep copies like a
container.

Another problem with boost::optional is that though it looks like a bit
like a pointer it does not require a dereference to construct it.

  void f( const T & );
  ...
  T x;
  f( *optional< T >( x ) );

This is not like any pointer I know there is an '&' missing. It would be
better if it was

  f( *optional< T >( &x ) );

But in my opinion better still is

  f( optional< T >( x ).value() );

It makes it clear that a copy has been made in the same way it is in the
following

  f( vector< T >( 1, x ).front() );

The second place I think it fits better is the implicit constructor.

  void f( void T & );

can be changed to

  void f( const optional< T > & );

without changing existing code that uses f.

Hamish


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