通过模板C++特征::矩阵类型进行转换
Issue casting C++ Eigen::Matrix types via templates
我正在编写一个在类型(float
或double
)上模板化的C++函数,并在内部使用Eigen::Matrix
。 该函数将使用float
、double
和模板化类型Eigen:Matrix
对象的组合。 Eigen::Matrix<>::cast()
适用于double
和float
,尽管我在将其与模板化类型一起使用时遇到了一个奇怪的问题。 请参阅下面的代码:
#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’
由于我只将模板实例化为Scalar
作为double
或float
,因此我想Scalar
函数调用应该与硬编码的float
/double
类型具有相同的效果。
更多系统信息:
- 乌班图14.04
- g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
- 特征码 3.2.4,从 http://eigen.tuxfamily.org/下载
提前感谢您的帮助!
谢谢,@piotr-s! 看起来这不是特定于 Eigen 的东西,而更一般地说只是一些用于调用模板化成员函数的棘手语法。
这里有一个相关的问题:如何调用模板成员函数?
答案如下:
mat_d = mat_s.template cast<double>();
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++中的双指针类型转换
- 逐位操作的隐式类型转换
- 模板中的类型转换
- 在 C++(和 C)中进行类型转换时明显不一致
- 字符类型转换不兼容
- 将复杂的非基元C++数据类型转换为 Erlang/Elixir 格式,以使用 NIF 导出方法
- C++:用户定义的显式类型转换函数错误
- 将类指针类型转换为键时出错
- 通过引用传递参数时C++类型转换
- 在 C++ 中将一个模板类型的对象类型转换为另一个模板类型
- C++显式类型转换(C 样式强制转换)的强制表示法和static_cast的多种解释
- C++无效的函数类型转换
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 如何将Windows产品类型转换为名称?
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 事件系统:使用类型转换或联合进行继承
- 如何在参数中定义隐式类型转换的构造函数?
- 类模板实例化中的类型转换