Eigen::Ref<Mat<T>> 的模板参数推导
Template argument deduction for Eigen::Ref<Mat<T>>
以下是模板参数推导的解决方法。
是否需要此解决方法?
这会阻止 Eigen 跨函数边界优化表达式吗?
#include <Eigen/Eigen>
#include <iostream>
template <typename T> using Mat = Eigen::Matrix<T, 3, 1>;
template <typename T = int>
inline void func(const Eigen::Ref<const Mat<T> >& a) {
// calculations
}
int main() {
const Mat<int> a;
const Eigen::Ref<const Mat<int> > b = a;
func(b); // error if I use func(a);
}
这在性能方面非常好,但是模板推导不起作用,所以要么将 func 设为非模板函数,要么将 Ref
替换为更通用的 const MatrixBase<Derived>& a
并使用静态断言来保护非 3x1 矩阵类型。在后一种情况下,最好在使用之前将 a
转换为它的真正派生类型:
template <typename Derived>
void func(const Eigen::MatrixBase<Derived>& a_base) {
const Derived& a(a_base.derived());
// calculations
}
以下是模板参数推导的解决方法。
是否需要此解决方法?
这会阻止 Eigen 跨函数边界优化表达式吗?
#include <Eigen/Eigen>
#include <iostream>
template <typename T> using Mat = Eigen::Matrix<T, 3, 1>;
template <typename T = int>
inline void func(const Eigen::Ref<const Mat<T> >& a) {
// calculations
}
int main() {
const Mat<int> a;
const Eigen::Ref<const Mat<int> > b = a;
func(b); // error if I use func(a);
}
这在性能方面非常好,但是模板推导不起作用,所以要么将 func 设为非模板函数,要么将 Ref
替换为更通用的 const MatrixBase<Derived>& a
并使用静态断言来保护非 3x1 矩阵类型。在后一种情况下,最好在使用之前将 a
转换为它的真正派生类型:
template <typename Derived>
void func(const Eigen::MatrixBase<Derived>& a_base) {
const Derived& a(a_base.derived());
// calculations
}