C ++ Eigen Listing Release :: Matrix Types via Templates

I am writing C ++ - a function that is templated by type (either floator double), and uses Eigen::Matrixinternally. The function will use a combination of objects float, doubleand templated type Eigen:Matrix. Eigen::Matrix<>::cast()works great for doubleand float, although I am using an odd issue when using patterns. See the following code:

#include "Eigen/Core"  // Version 3.2.4 (eigen-eigen-10219c95fe65)

template <typename Scalar>
void Foo() {
  Eigen::Matrix<double, 3, 1> mat_d = Eigen::Matrix<double, 3, 1>::Zero();
  Eigen::Matrix<float,  3, 1> mat_f = Eigen::Matrix<float,  3, 1>::Zero();
  Eigen::Matrix<Scalar, 3, 1> mat_s = Eigen::Matrix<Scalar, 3, 1>::Zero();

  mat_d = mat_f.cast<double>();  // Works
  mat_f = mat_f.cast<float>();   // Works

  mat_s = mat_f.cast<Scalar>();  // Works
  mat_s = mat_d.cast<Scalar>();  // Works

  mat_d = mat_s.cast<double>();  // Broken
  mat_f = mat_s.cast<float>();   // Broken
}

int main() {
  Foo<double>();
  Foo<float>();
}

Here is the compilation result:

> g++ casting.cpp
casting.cpp: In function ‘void Foo()’:
casting.cpp:16:22: error: expected primary-expression before ‘double’
   mat_d = mat_s.cast<double>();  // Broken
                      ^
casting.cpp:16:22: error: expected ‘;’ before ‘double
casting.cpp:17:22: error: expected primary-expression before ‘float
   mat_f = mat_s.cast<float>();   // Broken
                      ^
casting.cpp:17:22: error: expected ‘;’ before ‘float

Since I am only creating a template with Scalarlike doubleor float, I would suggest that function calls Scalarshould have the same effect as hardcoded float/ double.

Additional system information:

!

+4
1

, @piotr-s! , , - -.

: ?

:

mat_d = mat_s.template cast<double>();

+4

All Articles