特征-将const数组映射到动态向量 [英] Eigen - Map a const array to a dynamic vector
问题描述
我需要定义一个采用const
C数组并将其映射到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;
}
如果我从函数定义中删除了const
,则代码可以正常工作.但是是否可以保留const
而没有任何错误?
If I remove const
from the function definition the code works fine. But is it possible to retain the const
without any errors?
谢谢.
推荐答案
如果Map
的参数是非const
类型(例如Eigen::VectorXd
),则假定它可以修改原始缓冲区(在您的情况下为*data
).由于该函数需要一个const
合格的缓冲区,因此您必须告诉映射它是const
.将typedef
定义为
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;
它应该可以工作.
这篇关于特征-将const数组映射到动态向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!