Re: [eigen] Passing result of block() as non-const reference-to-matrix?

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


Am Freitag 09 Juli 2010, 15:46:51 schrieb Sidney Cadot:
> Hauke, Martin: thanks for your suggestions.
> 
> It's a pity that there is no clean solution that is portable to pre-C++0x.
> I still don't quite get that it does work as expected when I pass a const
> Block to a const Matrix & -- it is not clear for me why that /does/ work.

That's because const lvalue references are allowed to bind to temporary 
objects, while non-const lvalue references are not.
The reason is that a non-const reference parameter means that your function 
intends to modify the referenced object - otherwise you would use a const 
reference. However, modifying a temporary object usually doesn't make much 
sense and is usually a bug - therefore the C++ standard prevents you from 
doing so.

Furthermore, if you have a function
void f(const MatrixXd& m);
and call it with a MatrixXd::block(), then the block expression is converted 
to a plain matrix. That's okay since f only reads m and doesn't modify it.
If m was non-const, then converting the block object to a plain matrix would 
result in a bug, since the modification of the temporary object m inside f 
would not be visible outside of f, which is not what you indended.

By the way, even if your function does not modify its parameter, the function 
declaration
template <typename Derived>
void f(const MatrixBase<Derived>& m)
is more efficient, since it avoids the evaluation of the block expression to a 
plain matrix.

Cheers
Martin



> I have a feeling that I (as an API user) need to know too much about the
> intricacies of the Eigen data-structures in this case, while I can usually
> just really on stuff to (almost magically) work; A Matrix::block() result
> /usually/ returns something that behaves as Matrix, but not always.
> 
> Best regards, Sidney




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