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

标签 c++ eigen eigen3

这是我的示例代码:

(请注意#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 正确性的同时干净地绕过 Matrix 对象的好方法是什么?

例如,如果有一个带有成员函数的类:

const Matrix3d& SomeClass::getTheSuperDuperMatrix() const 
{
   return this->superDuperMat;
}

并且我计划在一个代码块中多次使用此函数的返回值,我想用一个短名称创建对它的引用,我希望它成为一个常量引用。

const Matrix3d& M = someClassInstance.getTheSuperDuperMatrix();

但是如果我将 M 传递给接受 const Ref<Matrix3d> 的函数,我将得到上面的编译错误参数,例如:

f1(M,x,y);

仅供引用,我目前使用的是以下版本的 Eigen:

Version: 3.2.91
Revision 5696:af94f93db432

最佳答案

这是因为 const Eigen::Ref<Eigen::MatrixXd>不是 const 引用。必须这样声明:

Eigen::Ref<const Eigen::MatrixXd>

关于c++ - 为什么 "const Eigen::Matrix<>&"和 "const Ref<Eigen::Matrix<>>"显然不兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20508697/

相关文章:

c++ - 加到 10 的简单递归函数

c++ - istream 用于 C++/Poco 的命令行输入

c++ - 将枚举作为参数传递

C++ 模板和 OpenBLAS

c++ - 用 Eigen 3 计算数组的特征值/vector 而不是矩阵

c++ - 没有用于初始化的匹配构造函数

eigen - 如何使用 Eigen 库用模板定义 Vector?

c++ - 将 C++ 数组映射到特征矩阵

c++11 - 特征对齐问题

c++ - 在 Eigen 中计算 SparseMatrix 1-范数、Inf-范数的有效方法