Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen - Map a const array to a dynamic vector

Tags:

c++

arrays

eigen

I need to define a function that takes a const C array and maps it into an Eigen map. The following code gives me an error:

double data[10] = {0.0};
typedef Eigen::Map<Eigen::VectorXd> MapVec;

MapVec fun(const double* data) {
  MapVec vec(data, n);
  return vec;
}

If I remove const from the function definition the code works fine. But is it possible to retain the const without any errors?

Thanks.

like image 953
user3294195 Avatar asked Nov 06 '16 08:11

user3294195


1 Answers

If the Map's parameter is a non-const type (e.Eigen::VectorXd) then it assumes that it can modify the raw buffer (in your case *data). As the function expects a const qualified buffer, you have to tell the map that it's const. Define your typedef as

typedef Eigen::Map<const Eigen::VectorXd> MapVec;

and it should work.

like image 58
Avi Ginsburg Avatar answered Nov 19 '22 02:11

Avi Ginsburg