Registered Member
|
I am struggling to do template specilaization for Eigen types.
Say I have a class
and then I want to specialize this class for Eigen::MatrixBase<Derived> types
But the above specialization will not work with say multiplyImpl<Eigen::Vector3d> since Eigen::Vector3d is not equal to Eigen::MatrixBase<Derived> but simply derives from it. This unfortunately is not good enough for template pattern matching. The way I know to solve this kind of problem is to do something as the following:
and the specialization as
Now my actual issue is being able to implement this is_eigen_type<T> which determines if T is an Eigen type. The way I am trying to solve this problem is to check if T derives from EigenDenseBase class as following:
Though this works for actual Eigen types e.g. is_eigen_type<Eigen::Vector2d> but will fail to compile when used for non-Eigen types like is_eigen_type<int>. This is because it tries to instantiate Eigen::EigenDenseBase<T> which fails for non-Eigen types. Something like is_eigen_type<T> or is_eigen_matrix_type<T> in my opinion is very useful. Am I missing some similar existing functionality already in Eigen? If not how do I go about this? |
Moderator
|
Cannot you make multiplyImpl a function for which matching template base classes does work?
|
Registered Member
|
Well my original intention is something like following:
and a partial specialization for Eigen Types like:
But since I cannot have partial specialization for functions, so I am trying to use the following approach as suggested here (Example 4: Illustrating Moral #2)
Let me know if there is a simpler approach. |
Moderator
|
You cannot have partial specializations but you can have overloads!
|
Moderator
|
BTW, why don't you use Eigen::Array instead of Eigen::Matrix if what you want is coefficient wise products?
|
Registered Member
|
I just gave that for simpler example. I am trying to write some generic functions that works on standard Scalar types like float, double and also on Eigen matrices and expressions. For e.g I am trying to write a function that converts probabilities to log-odds like:
Now I want to also do the same for float types i.e float log_odds = convert(const float& prob); I feel, ability to specialize for Eigen types is an important one if the users wants to write generic code that works with both Eigen Types and other types. |
Moderator
|
That's why I suggested to use Eigen::Array:
|
Registered Member
|
Thank a lot. That makes lot of the stuff I am writing right now more simpler. But a solution for template specialization will be great for future use.
|
Registered Member
|
Here is a Boost solution I am using:
Basically you can now use ENABLE_IF_EIGEN_TYPE(T) macro to specialize for Eigen Types like
With C++11, you can just replace boost::enable_if with std::enable_if. And C++11 version of is_base_of<Eigen::EigenBase<T>, T> should work with arbitrary T which makes the Boost MPL stuff unnecessary. @ggael and Eigen authors: A non-boost c++03 solution built into Eigen will be nice. What do you guys think? |
Registered users: Baidu [Spider], Bing [Bot], Google [Bot], rblackwell