|
Boost : |
Subject: Re: [boost] [optional] little addition... optional_move_to?
From: Niels Dekker - mail address until 2010-10-10 (niels_address_until_2010-10-10_at_[hidden])
Date: 2009-03-04 09:28:14
Fernando Cacciola wrote:
> That is, we can't just yet write that as:
> template<class T, class U> bool optional_assign( T&, optional<U>&& ) ;
> as we would a few years from now.
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:
template <class U>
bool optional_move_to(U& arg)
{
if (*this)
{
// Moves from **this to arg.
optional_detail::move_assign(arg, **this);
return true;
}
else
{
return false;
}
}
optional_detail::move_assign could be implemented by doing either
boost::swap (when T == U) or copy-assignment (when T != U), e.g., by
overloading:
template <class T>
void move_assign(T& lhs, T& rhs)
{
boost::swap(lhs, rhs);
}
template <class T, class U>
void move_assign(T& lhs, U& rhs)
{
lhs = rhs;
}
What do you think?
Arno, given such a optional<T>::optional_move_to member function, would
you still prefer to use your own optional_assign function? Otherwise
your use case could be written as follows:
boost::optional< TMyValue > TryToGetT();
T t;
if( ! TryToGetT().optional_move_to(t) ) {
... other way to get t ...
}
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