7

これが私のコード例です:

( で囲まれた部分に注意してください#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();

const Ref<Matrix3d>しかし、次のような引数を受け入れる関数に M を渡すと、上記のコンパイル エラーが発生します。

f1(M,x,y);

参考までに、現在、次のバージョンの Eigen を使用しています。

Version: 3.2.91
Revision 5696:af94f93db432
4

1 に答える 1

7

これはconst Eigen::Ref<Eigen::MatrixXd>const 参照ではないためです。次のように宣言する必要があります。

Eigen::Ref<const Eigen::MatrixXd>
于 2013-12-11T08:24:24.590 に答える