Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2008-08-23 14:41:44


on Sat Aug 23 2008, Niels Dekker - mail address until 2008-12-31 <nd_mail_address_valid_until_2008-12-31-AT-xs4all.nl> wrote:

>>> So can't this std::_Move_operation_category be
>>> specialized based on some template meta programming (e.g.,
>>> has_swap_member<T>::value)?
>
> David Abrahams wrote:
>> I don't know what member functions have to do with it; the real
>> criterion is whether or not there's a swap available for it via ADL.
>
> Thanks, Dave! I knew how to write has_swap_member<T> already, but I
> didn't yet know about has_swap_overload<T>. Now I see, it's also at
> http://svn.boost.org/svn/boost/sandbox/move/boost/move/detail/has_swap_overload.hpp

I think that might be an older version of the same functionality. In
fact, I think I forgot about the old version when I posted the one I
did. So you might compare and see if one is better.

> I just added another comment to the MSDN blog, mentioned your
> metafunction:
> http://blogs.msdn.com/vcblog/archive/2008/08/11/tr1-fixes-in-vc9-sp1.aspx
>
> It's a pity that has_swap_overload<T> yields false negatives, when T has
> std as associated namespace.

It's easy enough to fix that with specialization, as my posted example
shows.

> Maybe, as a workaround, VC9 SP1 could be
> tweaked to apply their "Swaptimization", when has_swap_overload<T> /or/
> has_swap_member<T> (using mpl::or_). Because I still think that if T
> has a swap member function, it's /very/ likely that it has a custom swap
> overload as well.

Well, now, that's not a bad point.

> Assuming that developers follow the "coding standard" to provide a
> non-member swap, whenever they provide a swap member function. What do
> you think?

IMO the best solution is like this:

    std is an associated namespace of T
    ? has_member_swap<T>
    : has_nonmember_swap<T>

plus any specializations required to handle something like tr1::array
that might not have a member swap. Rationale: if std is not an
associated namespace and the nonmember swap doesn't exist, then

  using std::swap;
  swap(x, y)

won't be optimized.

Thoughts?

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

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