Boost logo

Boost :

From: Hamish Mackenzie (hamish_at_[hidden])
Date: 2003-09-10 06:16:35


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.

Does anyone else prefer value to get? I think it will improve the
readability of code using optional< shared_ptr< T > >.

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.

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(); }

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 .

Hamish


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