通过模板发布C ++ Eigen :: Matrix类型 [英] Issue casting C++ Eigen::Matrix types via templates

查看:206
本文介绍了通过模板发布C ++ Eigen :: Matrix类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个以类型( float double )为模板的C ++函数,并使用内部 Eigen :: Matrix 。该函数将使用 float double 和模板类型 Eigen:Matrix的组合对象。 Eigen :: Matrix<> :: cast()对于 double float ,尽管在​​将其与模板类型一起使用时遇到了一个奇怪的问题。参见下面的代码:

I'm writing a C++ function that is templated on type (either float or double), and uses Eigen::Matrix internally. The function will be using a combination of float, double, and templated type Eigen:Matrix objects. Eigen::Matrix<>::cast() works just fine for double and float, though I'm hitting an odd issue when using it with templated types. See code below:

#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>();
}

这里是编译的结果:

> 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’

$ b之前
$ b

因为我只使用 Scalar 作为 double 实例化模板,或者 float ,我想想 Scalar 函数调用应该与硬编码的 float / double 类型。

Since I'm only ever instantiating the template with Scalar as a double or float, I'd imagine that the Scalar function calls should have the same effect as the hardcoded float/double types.

更多系统信息:

  • Ubuntu 14.04
  • g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
  • Eigen 3.2.4, downloaded from http://eigen.tuxfamily.org/

在此先感谢您的帮助!

推荐答案

谢谢,@ piotr-s!看起来这不是特定于Eigen的事情,但更一般而言,只是一些用于调用模板化成员函数的棘手语法。

Thanks, @piotr-s! Looks like this is not an Eigen-specific thing, but more generally just some tricky syntax for calling a templated member functions.

这里是一个相关的问题:如何调用模板成员函数?

Here's a related question: How to call a template member function?

这是答案:

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

这篇关于通过模板发布C ++ Eigen :: Matrix类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆