Boost logo

Boost :

From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2003-09-09 16:43:41


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.

This update includes mostly additional features so I believe its backward
compatible except for the member function get() that now returns a
reference instead of a pointer and the fact that code which used not
to compile now does.

These are the changes:

1.
The implementation has been refactored in order to ease
the implementation of alternative interfaces (since many
of you are working on that).
Boilerplate code and the ordinary interface is now on
optional_base<>, while the *conflictive* interface is in the
derived class optional<>.
AFAICT this refactoring does not affect in any way normal
users of optional<>. (there is no penalty whatsoever)

2.
The constructor taking a value is no longer explicit.
This allows you to use optional<> as optional arguments
transparently on the caller side:

void foo( int, optional<double> d = optional<double>() ) ;

void bar()
{
  foo(1);
  foo(2,3.4);
}

3.
An assignment operator taking a value is now available,
turning 'reset(T const&)' deprecated.
The presence of this operator makes optional<>
interoperate with tie smoothly.
For instance, the following is now possible:

optional<int> oi;
optional<double> od ;
tie(oi,od) = make_pair(1,2.3);

4.
The member and non-member functions get() now return
a reference instead of a pointer.
The NEW member function get_ptr() replaces the old get().
Also, following variant's interface, a NEW non-member
function get() taking a POINTER to optional<>
returns a pointer to T:

T* get ( optional<T>* opt ) ;

5.
References are now supported.
(using Eric Friedman's reference_content.hpp)
Since references cannot be rebound, optional<T&> is not assignable
and the conversion from optional<U&> to optional<T&> is not allowed
even if U is covertible to T (optional<U> is explicitely
convertible to optional<T> in that case).
Additionally, with optional<T&>, an attempt to call get_ptr()
results in a compile-time error.

6.
public typenames for metaprogramming:

optional<>::value_type
optional<>::argument_type
optional<>::reference_const_type
optional<>::reference_type
optional<>::pointer_const_type
optional<>::pointer_type
optional<>::is_reference_predicate

7.
Inplace construction is now supported.

If you include the new header:

<boost/utility/in_place_factory.hpp>

(which is NOT required for ordinary optional<> usage)

then you can initialize an optional constructing its contained
value directly in place instead of copying an existing object:

struct A
{
  A ( int a, std::string s ) ;
} ;

boost::optional<A> opt( boost::in_place(1234,"Hellow world!") ) ;

As you can see, the "factory expression" : in_place(...) does not
need the optional's value_type (A in this case), so the
syntax is terse.

I will upload the new headers and the new tests.

Fernando Cacciola


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