|
Boost : |
Subject: Re: [boost] [optional] little addition
From: Niels Dekker - mail address until 2010-10-10 (niels_address_until_2010-10-10_at_[hidden])
Date: 2009-03-03 18:00:51
>> 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 ...
>> }
Fernando Cacciola wrote:
> I like a swap much better, thanks Neil!
You're welcome!
> 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.
> 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). But
I wouldn't mind if it would be named differently, e.g.,
optional<T>::optional_swap(T&) :-)
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? Anyway, the main question remains: will any of those
"swap-if-initialized" functions be appreciated, or not...?
Kind regards, Niels
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk