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: Manoj Rajagopalan <rmanoj@xxxxxxxxx>
- Date: Mon, 5 Apr 2010 09:51:41 -0400
- Organization: EECS Dept., University of Michigan, Ann Arbor, MI, USA
Shall I file a bug report so this stays on the radar? Or can this be included
in a "Known Issues" section in the Eigen documentation? Using swap() in my
program made it buggy and since Eigen was new, I took no chances and
experimented with just about every construct I was using and discovered this
behavior. Others can benefit by not having to rediscover this.
Thanks,
Manoj
On Monday 05 April 2010 01:31:19 am joel falcou wrote:
> 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.
>
> Reference here:
>
> http://www.boost.org/doc/libs/1_42_0/boost/utility/swap.hpp