Boost logo

Boost :

Subject: [boost] [optional] std::tr2::optional
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2011-11-18 10:24:35

I use boost::optional quite often, find it very useful, and cannot imagine
that it could not get into TR2. I am considering writing a proposal for
adding it myself (if it is possible to propose things without attending the
committee meetings), but I would first want to know how boost::optional
could be improved given the new features in C++11. I provide a list of
modifications that I could think of. What is your opinion?

* Conversion explicit conversion to bool should be declared as explicit.

* Should exception safety guarantees for assignment operations be enforced?
In boost::optional they only provide a basic guarantee.
One option is to provide strong guarantee for types T which implement
operator= with strong guarantee, and give basic guarantee for other cases
Other option (not sure if it is possible) is to provide strong guarantee
for all cases at the cost of run-time performance (heap allocation).

* Add move operations
optional<T> & operator=( optional<T> && ) noexcept(
nothrow_move_constructible<T>::value );
optional<T> & operator=( T && ) noexcept(
nothrow_move_constructible<T>::value );

* move operations for optional references?
But I am not sure what optional references are for...

* What are optional references for anyway?

* it could add an rvalue reference overload for stored object access:
T&& operator*( ) &&
However using this overload implies that the user is not checking if an
optional has been initialized, so it may encourage a bad practice.

* Are in-place factories necessary if we have perfect forwarding? Instead,
we can provide a reset function and a "variadic constructor":

template< typename ... Args >
void reset( Args&& ...args );

template< typename ... Args >
optional( Args&& ...args );

The constructor will clash with another optional constructor:
optional<T>::optional( bool cond, const T& val );
but perhaps it is not needed if we have
std::optional<T> std::make_optional( bool cond,const T& val );
and move construction of optionals?

* noexcept default construction and destroy operations

* constexpr default ctor

* Use nullptr instead of boost::none? - this will make some uses of
optional pointers harder, but will make usage more clear in other cases.


Boost list run by bdawes at, gregod at, cpdaniel at, john at