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"
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>();
mat_f = mat_f.cast<float>();
mat_s = mat_f.cast<Scalar>();
mat_s = mat_d.cast<Scalar>();
mat_d = mat_s.cast<double>();
mat_f = mat_s.cast<float>();
}
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>();
^
casting.cpp:16:22: error: expected ‘;’ before ‘double’
casting.cpp:17:22: error: expected primary-expression before ‘float’
mat_f = mat_s.cast<float>();
^
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:
!