[Rcpp-devel] RcppEigen questions

Hao Xiong hao at biostat.ucsf.edu
Sat Nov 19 01:15:43 CET 2011


Hi,

I have a few questions about RcppEigen. I hope I am posting on the right 
list.

1. const-correctness

Suppose I have a function

double fcn(NumericVector const& points){
...
Map<VectorXd > tmp = as<Map<VectorXd > >(points);
...
}

It compiles fine.

Changing the mapping line to
Map<VectorXd const > tmp = as<Map<VectorXd const> >(points);
and it fails to compile.

Eigen is rather strict about const-correctness.
Map<VectorXd > tmp{const_pointer, 5};
would fail because the pointer is pointing to read-only memory. I am rather
confused as which way should be preferred.

Also the method begin() returns non-const pointer regardless whether the 
object
was declared const.

2. Still the same setup as above,

Map<ArrayXd > tmp = as<Map<ArrayXd > >(points);
fails to compile. Add const still results in failure. Is there anything 
special about mapping arrays that
I am not aware of?

3. I used to do the above in the following way

Map<ArrayXd> tmp{points.begin(), points.size()};

I think this is fine for dense matrix and array, but I am not entirely sure.
I would welcome any suggestion as to the pros and cons of this vs as<>().


Thanks for your help.
Hao


More information about the Rcpp-devel mailing list