Boost logo

Boost :

Subject: Re: [boost] [optional] little addition
From: Fernando Cacciola (fernando.cacciola_at_[hidden])
Date: 2009-03-03 09:34:19


Hi,

> Are you sure you want to do a copy-assignment of your T? Your example
> would typically be more efficient when doing a move-assignment or a swap
> instead. What would you think of an optional<T>::swap_if_initialized
> member function, so that you can do:
>
> if( ! TryToGetT().swap_if_initialized(t) {
> ... other way to get t ...
> }
>
I like a swap much better, thanks Neil!

I'm normally against obscure functions that are meant to save a few
keystrokes. OTOH, while the verbose alternative is perfectly clear and I
wouldn't even consider searching for a shortcut, this particular
shortcut looks clear enough to justify it.

So, Arno: don't you agree swap is better?

If so, does the swap() method really need the "_if_initialized"?

I would just add overloads to the existing swap:

  bool optional<T>::swap(T& a)

  bool swap( optional<T> const&x, T& y ) ;

  bool swap( T const&x, optional<T> const& y ) ;

I know 'swap' is a big name, but distorting it with a return value
doesn't seem disruptive enough to justify choosing a different name.

What do people think?

--
Fernando Cacciola
SciSoft Consulting
http://www.scisoft-consulting.com

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