特征-将const数组映射到动态向量 [英] Eigen - Map a const array to a dynamic vector

查看:93
本文介绍了特征-将const数组映射到动态向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定义一个采用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆