0

I am trying to create a template class with a memberfunction which can handle arithmetic datatypes (int, char, float ...) and a container-class like Eigen::DenseBase<> or std::vector<>

Code to demonstrate my idea:

template <typename T>class myClass{
  ...
  void foo(T);
  ...
};

template <typename T> void myClass<T>::foo(T){
  //Function for arithmetic Datatypes
}
//Specialization does not work - What is the correct (best?) approach?
template <> void myClass<T>::foo(<Eigen::DenseBase<T>){
  //Function for Eigen::DenseBase<T> - Objects
}

This are my first steps with template-programming so I am looking forward to tipps and ideas how to solve this problem

teflon
  • 15
  • 3
  • Could it be related to https://stackoverflow.com/questions/1723537/template-specialization-of-a-single-method-from-a-templated-class ? – Matthieu Brucher Nov 30 '18 at 15:34

1 Answers1

1

What you are trying to do is called partial specialization. You are trying to specialize your foo to work differently for a family of types - i.e. types which are instantions of Eigen::DenseBase. Unfortunately, this is not possible.

Member functions of template classes could only be fully-specialized, i.e. implementation could be provided for a specific type. For example, that would work:

    template <>
    void myClass<char*>::foo(char* );

The only way to partially specialize your foo is to put it into partial specialization for the whole class. Something like that:

template <typename T>
class myClass{
  ...
  void foo(T);
  ...
};

template<class T> 
class myClass<Eigen::DenseBase<T>> {
    void foo(Eigen::DenseBase<T> ) { ...}
};

The caveat here is that if you are (partially) specializing the class, you need to provide all the members which need to be there from the original template (often a lot of copy duplication). Standard solution here is to put everything which doesn't depend on partial specialization to the base class, and inherit your template and specialization from it.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • Wow! Great answer! Thank you very much for help! – teflon Nov 30 '18 at 15:56
  • @JonasFertsch not that I am begging for upvotes :), but here on Stack Overflow Upvotes/Answer acceptance is preferred over 'thank you' comments. – SergeyA Nov 30 '18 at 16:08