为什么"const Eigen::Matrix<>&"和"const Ref<Eigen::Matrix<> >"显然不兼容?
Why are "const Eigen::Matrix<>&" and "const Ref<Eigen::Matrix<> >" apparently incompatible?
下面是我的示例代码:
(请注意#if ENABLE_MY_COMPILE_ERROR
包围的部分)
#include <Eigen/Core>
#include <iostream>
#define ENABLE_MY_COMPILE_ERROR 1
void f1(const Eigen::Ref<Eigen::MatrixXd> a,
const Eigen::Ref<Eigen::MatrixXd> b,
Eigen::Ref<Eigen::MatrixXd> c)
{
c = a * b;
}
int main(int argc, const char *argv[])
{
Eigen::Matrix3d M;
Eigen::Vector3d x;
Eigen::Vector3d y;
M.setRandom();
x.setRandom();
std::cout<<"M = n"<<M<<std::endl;
std::cout<<"x = n"<<x<<std::endl;
std::cout<<"M * x = n"<<M * x<<std::endl;
{
y.setZero();
f1(M,x,y);
std::cout<<"y = n"<<y<<std::endl;
}
{
Eigen::Matrix3d& MRef = M;
y.setZero();
f1(MRef,x,y);
std::cout<<"y = n"<<y<<std::endl;
}
#if ENABLE_MY_COMPILE_ERROR
{
const Eigen::Matrix3d& MRef = M;
y.setZero();
f1(MRef,x,y);
std::cout<<"y = n"<<y<<std::endl;
}
#endif
}
以下是我在ENABLE_MY_COMPILE_ERROR != 0
:时得到的编译错误
In file included from ../../../../external/src/eigen-current/Eigen/Core:334:0,
from Eigen_Ref.C:1:
../../../../external/src/eigen-current/Eigen/src/Core/PlainObjectBase.h: In constructor ‘Eigen::Ref<PlainObjectType, Options, StrideType>::Ref(const Eigen::DenseBase<OtherDerived>&, typename Eigen::internal::enable_if<(bool)((Eigen::internal::is_lvalue<Derived>::value && (bool)(typename Eigen::internal::traits<Eigen::Ref<_PlainObjectType, _Options, _StrideType> >::match<Derived>::MatchAtCompileTime))), Derived>::type*, int) [with Derived = Eigen::Matrix<double, 3, 3>; PlainObjectType = Eigen::Matrix<double, -1, -1>; int Options = 0; StrideType = Eigen::OuterStride<>; typename Eigen::internal::enable_if<(bool)((Eigen::internal::is_lvalue<Derived>::value && (bool)(typename Eigen::internal::traits<Eigen::Ref<_PlainObjectType, _Options, _StrideType> >::match<Derived>::MatchAtCompileTime))), Derived>::type = Eigen::Matrix<double, 3, 3>]’:
../../../../external/src/eigen-current/Eigen/src/Core/PlainObjectBase.h:726:12: error: ‘Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3> >::<anonymous enum> Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3> >::ThisConstantIsPrivateInPlainObjectBase’ is private
Eigen_Ref.C:47:18: error: within this context
因此,显然以下内容是不兼容的:
const Eigen::Ref<Eigen::MatrixXd> a
const Eigen::Matrix3d& MRef = M
我的问题是:
这是设计的,还是Eigen::Ref<>的错误/缺点?
如果是设计的,那么在保持常量正确性的同时,什么是干净地传递矩阵对象的好方法呢?
例如,如果有一个具有成员函数的类:
const Matrix3d& SomeClass::getTheSuperDuperMatrix() const
{
return this->superDuperMat;
}
我计划在一个代码块中多次使用这个函数的返回值,我想用一个短名称创建一个对它的引用,我希望它是一个常量引用。
const Matrix3d& M = someClassInstance.getTheSuperDuperMatrix();
但是,如果我将M传递到接受const Ref<Matrix3d>
参数的函数中,则会出现上述编译错误,例如:
f1(M,x,y);
仅供参考,我目前正在使用以下版本的特征:
Version: 3.2.91
Revision 5696:af94f93db432
const Eigen::Ref<Eigen::MatrixXd>
不是常量引用。必须这样声明:
Eigen::Ref<const Eigen::MatrixXd>
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中