Re: [eigen] std::swap doesn't work on Map

[ Thread Index | Date Index | More Archives ]

Benoit Jacob wrote:
Normally we could consider fixing this by providing a partial template
specialization of std::swap for Eigen types, but the big problem is
that std::swap takes only 1 template parameter and expects both sides
to be of the same type, and this is not always the case with Eigen
swap on expressions. We could still catch the case where both sides
have the same type, but I'm not sure what to do then. If we let that
work, it's awkward that we require both sides to have the same type in
std::swap and not in .swap(). If we want to emit an error, it's hard
to do because of SFINAE (so generating an error there would simply
discard the specialization.). I have to read back the rules of SFINAE,
perhaps it will work to let our std::swap specialization call another
function from where we trigger an error...

Look how boost::swap works, there is a simple trick to have your own eigen::swap that behaves correctly
without tricky SFINAE. Basically :

namespace eigen {
namespace details
 template<class T1,class T2>
 void swap( T1& a, T2& b)
   using std::swap;
swap(a,b); }

template<class T1,class T2>
void swap( T1& a, T2& b)


the main swap having *two* templates arguments make it more specialized than the ADL found swap for type T1 and T2. Then the using clause in the details::swap bring std::swap in the game if and only if the proper swap can't be found by ADL.

Using this swap then behaves like that:
if a swap exists in the namespace of T1, it is used.
else std::swap is used silently.

Now, just tell user to use eigen::swap.

Reference here:

Joel Falcou - Assistant Professor
PARALL Team - LRI - Universite Paris Sud XI
Tel : (+33)1 69 15 66 35

Mail converted by MHonArc 2.6.19+