Skip to content

[Rcpp-devel] problem compiling with const in RcppEigen

2 messages · M A, Dirk Eddelbuettel

M A
#
Yes, I can understand why it's not clear what the Cm_class is for (I
did take out all the linear algebra and other parts of the class).
That is, after all, a consequence of creating a minimal example. And,
I am also happy to declare functions as you do. The point is, though,
that the typedef used in the function definition does not work with
the constructor for the Cm_class, even though they are conceptually
the same thing. I appreciate your interest in what the purpose of the
code is, but it's probably not worth trying to explain the reasons for
why it's designed they way it is. I can already work around the
problem, for instance by using two different typedefs and making sure
they mesh properly at the right points (though it's annoying to have
two different typedefs for a single conceptual thing), or other things
I mentioned previously.

The fundamental problem is, as you said, that RcppEigen doesn't know
how to form an Eigen::Map<const Eigen::MatrixXd> with the as function.
Unfortunately, this is the only way of creating a read-only mapping
from a constant pointer (i.e. const double*) in Eigen (see below
code). So, when 'as' operates I need to use a const Map<MatrixXd>, but
when I want to create my own read-only mapping from a constant pointer
I need a Map< const MatrixXd>, hence two different typedefs even
though they're really supposed to be the same thing.

In Eigen this works:
const double *p=&somearray;
Map<const MatrixXd> mapping(p, n, n);

In Eigen this doesn't work: (maybe there's an argument to make Eigen allow this)
const double *p=&somearray;
const Map<MatrixXd> mapping(p, n, n);
On Thu, Jan 31, 2013 at 2:38 PM, Douglas Bates <bates at stat.wisc.edu> wrote:
#
On 1 February 2013 at 10:26, M A wrote:
| The fundamental problem is, as you said, that RcppEigen doesn't know
| how to form an Eigen::Map<const Eigen::MatrixXd> with the as function.

Templates are initiated by the compiler based on what is available.  This
fails as nobody wrote one with this 'const' signature.  Now, if you want and
need it, maybe you could supply it?

The nice thing is that you can even do this locally first and test, and, if
you're so inclined, contribute it as a patch to RcppEigen later.  

Dirk