Re: [eigen] Ref<> Problem |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
On 05.12.2014 10:22, Gael Guennebaud wrote:
On Fri, Dec 5, 2014 at 1:21 AM, gabriel nützi <gnuetzi@xxxxxxxxx> wrote:
Ref<MatrixXd> a; is a reference to a MatrixXd and “a” should
not in any circumstances bind to some kind of temporary Eigen object
(expression/block, what ever?) (like double & a;)
Just to be clear: Ref<MatrixXd> can bind to Blocks or Maps, etc if they
are writable expressions and storage layout is compatible:
MatrixXd A(...);
double *aPtr = ...;
const MatrixXd B(...);
const double *bPtr = ...;
Ref<MatrixXd> Ablock = A.block(...); // ok
Ref<MatrixXd> Amap = MatrixXd::Map(aPtr, ...); // ok
Ref<MatrixXd> Bblock = B.block(...); // does not work
Ref<MatrixXd> Bmap = MatrixXd::Map(bPtr, ...); // does not work
Ref< const MatrixXd> a; is a reference to a MatrixXd which is CONSt and
might bind to a temporary Eigen object (expression/block, what ever?) (like
const double &)
Ref<const MatrixXd> can reference to all of the above, without using its
temporary. It also can reference to expressions which are not
direct-access and are evaluated into a temporary (e.g. A+2*B).
So generally, like C++ references, Ref should be considered as
initialize-once variables. And, like in C++, strange things can happen, if
the variable by which it got initialized runs out of scope before the
reference does.
These pitfalls go away if the creation of a temporary is disabled in the
Ref<> object (bug 884) which seems to be OK in Gabriel's use case.
The pitfalls which we have with standard C++ references will still exist
(referencing an object which gets modified or out of scope while the Ref
still exists). But we won't be able to do anything about that.
Christoph
--
----------------------------------------------
Dipl.-Inf., Dipl.-Math. Christoph Hertzberg
Cartesium 0.049
Universität Bremen
Enrique-Schmidt-Straße 5
28359 Bremen
Tel: +49 (421) 218-64252
----------------------------------------------