【发布时间】:2019-04-03 09:24:01
【问题描述】:
我正在尝试编写一个以Eigen::Tensor 作为参数的模板函数。适用于Eigen::Matrix 等的相同方法在这里不起作用。
Eigen 建议使用通用基类编写函数。 https://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html
编译的Eigen::Matrix 的最小示例:
#include <Eigen/Dense>
template <typename Derived>
void func(Eigen::MatrixBase<Derived>& a)
{
a *= 2;
}
int main()
{
Eigen::Matrix<int, 2, 2> matrix;
func(matrix);
}
以及无法编译的Eigen::Tensor的最小示例:
#include <unsupported/Eigen/CXX11/Tensor>
template <typename Derived>
void func(Eigen::TensorBase<Derived>& a)
{
a *= 2;
}
int main()
{
Eigen::Tensor<int, 1> tensor;
func(tensor);
}
$ g++ -std=c++11 -I /usr/include/eigen3 eigen_tensor_func.cpp
eigen_tensor_func.cpp: In function ‘int main()’:
eigen_tensor_func.cpp:12:16: error: no matching function for call to ‘func(Eigen::Tensor<int, 1>&)’
func(tensor);
^
eigen_tensor_func.cpp:4:6: note: candidate: ‘template<class Derived> void func(Eigen::TensorBase<Derived>&)’
void func(Eigen::TensorBase<Derived>& a)
^~~~
eigen_tensor_func.cpp:4:6: note: template argument deduction/substitution failed:
eigen_tensor_func.cpp:12:16: note: ‘Eigen::TensorBase<Derived>’ is an ambiguous base class of ‘Eigen::Tensor<int, 1>’
func(tensor);
【问题讨论】:
标签: c++ templates tensor eigen3