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

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]

*To*: eigen@xxxxxxxxxxxxxxxxxxx*Subject*: Re: [eigen] std::swap doesn't work on Map*From*: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>*Date*: Mon, 5 Apr 2010 17:55:19 -0400*Dkim-signature*: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:received:message-id:subject:from:to:content-type :content-transfer-encoding; bh=/eUZhgA05lXdFoX1sV37VZYpm714xuAx2jc6dChqoTE=; b=yABS0GxFftD4F2heb24OryspsvAissZsaJuGuE3Ivzr1rY8wv5Nq9f70wktKjEwfh4 ifIx3d0b/BL4Z2K0liKqAdDEYwrP92Fsgs1RBJ5vEbAUUbmhXDi8vc4/5C2Or60NJfOv wzds0CxsA/Y6E8IoMudW/JH4WZNgbHdhAF3sg=*Domainkey-signature*: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=d60/SeSGxiGsTKXf9IdFPhpkgdhDmHYFn7kuvYTw0rfIvHLjl4q7x/SEIl9xhqQYy7 gpRL06svFQspDD26l3G39cwmatEiwJfQUfOnKxIXNE7R03Y2sk97AitaPK6RdC9nvLsE gzAOLNIwFPzIZIMm1C4eO4fmmdCwowXwokf/o=

Hi Joel, 2010/4/5 joel falcou <joel.falcou@xxxxxx>: > 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... >> >> Benoit >> > > 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) > { > details::swap(a,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. We already have a swap() that works well in Eigen, depending on two template parameters. It has a different calling syntax: it is a member function. a.swap(b). Here the problem is with users who expect std::swap to work on the Map expression (where it is really tricky), and discover that it doesn't. If we decide that it's OK to tell them to use a different swap() function, then Eigen's current solution is already good enough. I was wondering if we could do something to prevent the bad surprise with std::swap from happening in the first place. I'll try to get a static assertion to work, with an informative error message. Benoit > > Reference here: > > http://www.boost.org/doc/libs/1_42_0/boost/utility/swap.hpp > > > > -- > ___________________________________________ > Joel Falcou - Assistant Professor > PARALL Team - LRI - Universite Paris Sud XI > Tel : (+33)1 69 15 66 35 > > > > >

**Follow-Ups**:**Re: [eigen] std::swap doesn't work on Map***From:*Eamon Nerbonne

**Re: [eigen] std::swap doesn't work on Map***From:*joel falcou

**References**:**[eigen] std::swap doesn't work on Map***From:*Manoj Rajagopalan

**Re: [eigen] std::swap doesn't work on Map***From:*Benoit Jacob

**Re: [eigen] std::swap doesn't work on Map***From:*joel falcou

**Messages sorted by:**[ date | thread ]- Prev by Date:
**Re: [eigen] std::swap doesn't work on Map** - Next by Date:
**Re: [eigen] std::swap doesn't work on Map** - Previous by thread:
**Re: [eigen] std::swap doesn't work on Map** - Next by thread:
**Re: [eigen] std::swap doesn't work on Map**

Mail converted by MHonArc 2.6.19+ | http://listengine.tuxfamily.org/ |