Re: [eigen] MatrixBase::swap -- why const?

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


the problem is that c++ doesn't seem to allow to pass a non-constant
reference to a temporary.

Right. This problem is solved by rvalue references in C++0x. I believe what you really want is:

template<typename OtherDerived>
void swap(MatrixBase<OtherDerived>&& other)

which will bind to temporaries as well as lvalue references. Similarly for lazyAssign, and anywhere else you're currently abusing const_casts to support temporaries. Of course rvalue references are only supported by the very latest compilers :(

Using const reference arguments and const_cast is the simplest solution, but has some serious drawbacks. It is indeed a breach of contract. It works in all the cases you want it to work, but also ones you don't!

It's possible to emulate the "move semantics" of rvalue references in pure C++03 to some degree, and there's been a lot of discussion on the Boost mailing list about this. If you want some inspiration, or a headache, check out at least the first couple sections of the docs for the proposed Boost.Move macros library. The BOOST_ENABLE_MOVE_EMULATION and BOOST_RV_REF expand to the idiomatic C++0x solution (if possible), or to a close C++03 approximation. A drawback to emulation is you'd need to use swap on temporaries like this:

matrix.row(i).swap( move(matrix.row(j)) )

which is more verbose with the "move" call but makes permission to modify the object explicit.

Another option is to stick with the current solution but use rvalue refs when available:

#ifdef HAVE_CPP0X
#define EIGEN_RV_REF(type) type&&
#else
#define EIGEN_RV_REF(type) const type&
#endif

template<typename OtherDerived>
void swap( EIGEN_RV_REF(MatrixBase<OtherDerived>) other )

That way at least the early adopters can have their type safety. Probably another macro is needed to deal with the const_casting; I'm too tired to work out the full details.

swap() for heterogeneous types makes me a bit uneasy, but maybe it is OK. I'll have to take a closer look later.

-Patrick


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