Boost logo

Boost :

Subject: Re: [boost] [optional] little addition... optional_assign_to, optional_move_to, optional_swap?
From: Niels Dekker - mail address until 2010-10-10 (niels_address_until_2010-10-10_at_[hidden])
Date: 2009-03-06 18:22:22


Arno Schödl wrote:
> I am happy with members.

I'm glad we've reached a consensus :-) Would you like to officially
  create a request ticket for those three member functions, at
https://svn.boost.org/trac/boost/newticket ? Otherwise I wouldn't mind
doing so.

> Just for my understanding, why are optional_swap and
> optional_move_to not expressible as free functions?
>
> template< class T, class U >
> bool optional_move( T& t, boost::optional<U>& u ) {
> if( u ) {
> t=*u;
> return true;
> } else {
> return false;
> }
> };
>
> template< class T >
> bool optional_move( T& t, boost::optional<T>& u ) {
> if( u ) {
> swap( t, *u );
> return true;
> } else {
> return false;
> }
> };
>
>
> {
> int i;
> boost::optional<int> j=1;
> optional_move( i, j ); // swaps
> }
> {
> double i;
> boost::optional<int> j=1;
> optional_move( i, j ); // assigns with implicit conversion
> }
>
> What am I missing?

Your original use case, from your very first posting on this subject!

   boost::optional< TMyValue > TryToGetT();
   T t;

   // Would not compile, trying to pass an optional<T> rvalue!
   if( !optional_move( t, TryToGetT () ) {
     // ... other way to get t ...
   }

The following would compile though, even if
optional<T>::optional_move_to(T&) is non-const:

   if( ! TryToGetT().optional_move_to(t) {
     // ... other way to get t ...
   }

HTH, Niels


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