Boost logo

Boost :

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

Hi Niels,

>> 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?
> Would a function like optional<T>::swap_if_initialized(T&) indeed be the
> preferable one for Arno's use case? It isn't yet entirely clear to me,
> as it seems that Arno would like to have an implicit conversion, while
> passing the value from optional<T> to its target.
I actually didn't notice that the first time. Of course swap() can do
the same via "optional<T>::swap_if_initialized(U&)" for that matter.

>> 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.
> Personally, I would prefer to have such functions named slightly
> different from "swap", to make clear that they're not regular swap
> function. You know, people expect a swap member function to /always/
> successfully exchange the values, while swap_if_initialized would /only/
> exchanges the values when optional<T> is "initialized" (non-empty).

OK, that's a very good argument.

> But I wouldn't mind if it would be named differently, e.g.,
> optional<T>::optional_swap(T&) :-)

I think I like this one better.

> IMO a "swap-if-initialized" /member/ function might be especially useful
> because it allows swapping an rvalue (optionally).


> I'm not sure if free
> "swap-if-initialized" function overloads would also be needed. What do
> you think?

As it won't be just swap(), it doesn't need to be a free function, and
right now at least I can't see any reason for having it.

> Anyway, the main question remains: will any of those
> "swap-if-initialized" functions be appreciated, or not...?

I do, so if no one points out a good reason for adding it, I will.

As I said before I really hate bloated interfaces with this seems
sufficiently useful and syntactically clear.


Fernando Cacciola
SciSoft Consulting

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