Boost logo

Boost :

From: Hamish Mackenzie (hamish_at_[hidden])
Date: 2003-09-10 18:11:23


On Wed, 2003-09-10 at 17:05, Fernando Cacciola wrote:
> >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.

True. I am convinced.

> > 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!

Sweet. Though now you have to get the US and UK to agree on how to
spell it :-)

> I see your point but I'm not sure if this is a recommended pattern.

It also allows the function to be used with classes without copy
constructors

  template< typename T >
  void f( const optional< T > & );

restricts T to copyable classes.

I think optional< T > is the middle of three cases

  T // Known to exist
  optional< T > // May or may not exist
  null_optional< T > // Known not to exist

null_optional would have the same interface as optional but would
need no storage overhead.

There would be an implicit constructor to make an optional< T > from
null_optional< T > and optimised versions of the optional_empty and
optional_get functions.

> 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.

Yes this is a problem. It would be handy to have a way to
check the type so

  void f( const optional< some_big_class > & );

would become

  template< typename T >
  void f( const T & )
  {
    BOOST_STATIC_ASSERT(
      is_optional_version_of< some_big_class, T >::value );
    ...
  }

Perhaps implemented using an optional_traits class.

Hamish


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