R zu
R zu

Reputation: 2064

Template argument deduction for Eigen::Ref<Mat<T>>

The following is a workaround for template argument deduction.

Is this workaround necessary?

Will this prevent Eigen from optimizing expression across the boundary of func?

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

Upvotes: 0

Views: 163

Answers (1)

ggael
ggael

Reputation: 29205

This is perfectly fine regarding performance, but template deduction won't work, so either make func a non template function, or replace Ref by a more general const MatrixBase<Derived>& a and use static assertions to protect for non 3x1 matrix type. In the latter case, better cast a to its true derived type before using it:

template <typename Derived>
void func(const Eigen::MatrixBase<Derived>& a_base) {
  const Derived& a(a_base.derived());
  // calculations
}

Upvotes: 1

Related Questions