dimanche 24 décembre 2017

Template Specialization Function with Eigen arguments

I am trying to write some functions with template specialization for Eigen types, following this question: Eigen: type deduction in template specialization of base-class

I wrote this:

#include <type_traits>
#include <Eigen/Core>

namespace isEigenPlainBaseObjectDetail {
    template <typename T>
    std::true_type test(const Eigen::PlainObjectBase<T>&);
    std::false_type test(...);
}

template <typename T>
struct isEigenPlainBaseObject :
        public decltype(isEigenPlainBaseObjectDetail::test(std::declval<T>())) {};

template <typename T, typename Enable = void>
void foo(T& obj) {
    std::cout << "Generic Called!\n";
}

template <typename T, typename std::enable_if<isEigenPlainBaseObject<T>::value>::type>
void foo(T& obj) {
    std::cout << "Eigen Specialization Called!\n";
}

int main() {
    Eigen::MatrixXd m;
    Eigen::VectorXd v;
    int i = 0;
    foo(i);
    foo(m);
    foo(v);
    return 0;
}

but this calls everytime the generic function:

Generic Called!
Generic Called!
Generic Called!

What am I doing wrong? How can I write template specializations for functions with Eigen matrices and vectors?

Aucun commentaire:

Enregistrer un commentaire