编译错误 将模板编程与特征C++库结合使用

Compilation Error Using template programming with Eigen C++ library

本文关键字:C++ 结合 特征 错误 编程 编译      更新时间:2023-10-16

我下载了Eigen(3)库并开始使用它。我编写了一个模板函数,并在函数内声明了一个"模板类型"的局部变量。我收到以下编译错误。


$ g++ EigenTest.cpp
EigenTest.cpp: In instantiation of ‘void myFunc(Eigen::MatrixBase<Derived>&) [with Type1 = Eigen::Matrix<double, -1, -1>]’:
EigenTest.cpp:24:10:   required from here
EigenTest.cpp:16:26: error: conversion from ‘Eigen::DenseCoeffsBase<Eigen::Matrix<double, -1, -1>, 1>::Scalar {aka double}’ to non-scalar type ‘Eigen::Matrix<double, -1, -1>’ requested
   Type1 tmp = matrix(0, 0);

"特征测试.cpp"如下。


#include "Eigen/Dense"
#include <iostream>
template<typename Type1>
void myFunc(Eigen::MatrixBase<Type1>& matrix)
{
int i=matrix.rows();
Type1 tmp = matrix(0, 0);           // getting compiler error here
std::cout<<"tmp is ->"<<tmp<<std::endl;
}
int main()
{
Eigen::MatrixXd m(2,2);
m.setConstant(100); 
myFunc(m);
return 0;
}

我还尝试使用"typename Type1 tmp = matrix(0, 0);"
这也行不通!

如何解决这个问题?在正常的C++模板编程(没有 Eigen)中,我可以在模板函数中定义一个局部变量作为 'Type1 tmp;"

Eigen::MatrixBase<Type1>中,Type1不是标量类型,而是实际表达式的类型。在你的例子中,它将是MatrixXd,但如果调用myFunc,例如m.block(...),那么Type1将是一个Block<...>。若要获取标量类型,可以使用 Type1::Scalar:

template<typename Type1>
void myFunc(Eigen::MatrixBase<Type1>& matrix)
{
  typename Type1::Scalar Scalar;
  Scalar tmp = matrix(0, 0);
}

如果您需要类似于Type1的矩阵类型,请使用Type1::PlainObject,例如:

typename Type1::PlainObject mat = 2 * matrix * matrix.transpose();

看起来MatrixBase使用"CRTP"(见这里),模板参数实际上是从它派生的类型。 因此,在你使用方法myFunc()时,Type1实际上是在表示Eigen::MatrixXd我认为认为Type1是一个双精度。 所以,这一行:

Type1 tmp = matrix(0, 0);

在这个库的文档中(见这里),MatrixXd的 typedef 是一个双精度矩阵,所以我想从 matrix(0, 0) 返回是一个双精度,并且由于 tmp 是Type1 Eigen::MatrixXd,一个不会进入另一个。

扫描文档我认为您的函数最好将Matrix作为参数,这样标量类型应该可用。 像这样:

template<class T, int rows, int cols, int opts, int maxR, int maxC > 
void myFunc( Eigen::Matrix<T, rows, cols, opts, maxR, maxC>& matrix )
{
    T tmp = matrix(0, 0);
}

(虽然看起来很可怕!! ;-) )

在你的代码中,Type1被推导为double(因为Eigen::MatrixXd是这样定义的)。

然后你正在尝试做

Type1 tmp = matrix(0, 0);

而且恐怕我的本征知识还不够,所以我通过 Clang 3.3 运行它,并得到这个错误:

test.cpp:9:7: error: no viable conversion from 'Scalar' (aka 'double') to
      'Eigen::Matrix<double, -1, -1, 0, -1, -1>'
Type1 tmp = matrix(0, 0);           // getting compiler error here
      ^     ~~~~~~~~~~~~
test.cpp:17:1: note: in instantiation of function template specialization
      'myFunc<Eigen::Matrix<double, -1, -1, 0, -1, -1> >' requested here
myFunc(m);
^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:210:5: note: candidate constructor not viable:
      no known conversion from 'Scalar' (aka 'double') to
      'internal::constructor_without_unaligned_array_assert' for 1st argument
    Matrix(internal::constructor_without_unaligned_array_assert)
    ^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:284:25: note: candidate constructor not
      viable: no known conversion from 'Scalar' (aka 'double') to 'const
      Eigen::Matrix<double, -1, -1, 0, -1, -1> &' for 1st argument
    EIGEN_STRONG_INLINE Matrix(const Matrix& other)
                        ^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:272:25: note: candidate template ignored:
      could not match 'MatrixBase<type-parameter-0-0>' against 'double'
    EIGEN_STRONG_INLINE Matrix(const MatrixBase<OtherDerived>& other)
                        ^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:292:25: note: candidate template ignored:
      could not match 'ReturnByValue<type-parameter-0-0>' against 'double'
    EIGEN_STRONG_INLINE Matrix(const ReturnByValue<OtherDerived>& other)
                        ^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:303:25: note: candidate template ignored:
      could not match 'EigenBase<type-parameter-0-0>' against 'double'
    EIGEN_STRONG_INLINE Matrix(const EigenBase<OtherDerived> &other)
                        ^
1 error generated.

这告诉我你不能这样称呼matrix,用两个0作为参数。这也是奇怪的语法,因为MatrixBase类没有您似乎试图调用的operator()