Boost logo

Boost :

Subject: Re: [boost] [non-instrusive move] interest in new library?
From: Thomas Jordan (tomjordan766_at_[hidden])
Date: 2013-01-11 05:07:37

Joseph Gauterin wrote:
> Out of curiosity, how are you detecting if a member function swap exists?
>> it detects at compile-time if the type of the
>> argument(s) has a member swap function
//Type trait to determine whether a type has a swap() member function
//Uses SFINAE and the "sizeof trick" from the book "C++ Template
//by Abrahams and Gurtovoy

template<typename T>
struct has_swap
     typedef char yes;
     typedef char (&no)[2];

     //SFINAE eliminates this when the type is invalid
     template <typename U, U>
     struct Check;

     //this overload will be selected if the Check type is valid
     template <typename U>
     static yes Tester(Check<void(U::*)(U&), &U::swap>*);

     //overload resolution prefers anything at all over ...
     template <typename U>
     static no Tester(...);

     static bool const value = sizeof(Tester<T>(0)) == sizeof(yes);

> In general, there's no way of knowing if a member swap function is
> more efficient than a single assignment. It isn't even for all the
> examples you gave - boost::array<T,N>::swap performs N boost::swaps,
> which, if T doesn't provide an efficient swap, results in N calls to
> std::swap, and 3*N assignments. In general I'm wary of using swap in a
> generic context unless the intent is to swap (i.e. not as a potential
> optimization of assignment).
Thanks, point taken. I've responded to the exact same point in Jeffrey
Lee Hellrung Jr.'s
comment on my original post, and also made some alternative suggestions.
certainly appreciate any further feedback you have there.

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