为什么"const Eigen::Matrix<>&"和"const Ref<Eigen::Matrix<> >"显然不兼容?

Why are "const Eigen::Matrix<>&" and "const Ref<Eigen::Matrix<> >" apparently incompatible?

本文关键字:gt lt Matrix const Eigen 不兼容 为什么 Ref      更新时间:2023-10-16

下面是我的示例代码:

(请注意#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>