
Well, we can achieve almost the same in old C++03, by declaring the function as a /member/ of optional<T>. I'd suggest naming it "optional_move_to", as follows:
Fernando Cacciola wrote:
Nice one! :)
Thanks :-)
I keep wondering whether being member functions, these move_to() and swap() need the "optional_" name prefix?
In the case of "optional_move_to", "move_to" is okay to me as well, as "move_to" isn't so much "overloaded". (Although I still think that "optional_move_to", or even "conditional_move_to", is more clear.)
On further thinking, if the optional is an lvalue this would do the right thing but will have the wrong name o)
If "move_to" does either swap or assign-to, IMO the name is right, even if it moves from an lvalue.
Or, probably better, a single [optional_]assign_to() that magically does one or the other depending on "this" being and lvalue or rvalue reference
Something which, btw, in C++0x can actually be properly overloaded:
template<class U> bool optional<T>::assign_to( U& ) & ; template<class U> bool optional<T>::assign_to( U& ) && ;
This is getting unsurprisingly closer to the initial proposal from Arno :)
What about adding a "const" overload as well? ;-) template<class U> bool optional<T>::assign_to( U& ) const & ; Maybe Ion GaztaƱaga's Boost.Move can be of help to emulate move semantics in C++03? http://svn.boost.org/svn/boost/sandbox/libs/move_semantics/doc/html/index.ht... Anyway, for the time being I think that adding optional_move_to(T&) and optional_swap(T&) members (as I presented), and optional_assign (by Arno) should be sufficient. And I still wonder if Arno would be happy to use optional_move_to, instead of his optional_assign... Kind regards, Niels