This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Traits specialization for Eigen types

Tags: None
(comma "," separated)
RhysU
Registered Member
Posts
5
Karma
0
Would you please suggest a way to get a template specialization for Eigen
types? I'd like to put together a few traits classes that work for both Eigen
and non-Eigen types.

Here's a working example that uses some Boost functionality:
Code: Select all
#include <iostream>
#include <Eigen/Core>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_base_of.hpp>

using namespace std;

template<class T, class Enable = void>
struct Trait {
    static const int x = 0;
};

template< class T >
struct Trait<T, typename boost::enable_if<
                    boost::is_base_of<Eigen::DenseBase<T>, T>
                >::type
> {
    static const int x = 1;
};

int main(int argc, char *argv[])
{
    cout << Trait<Eigen::Vector3i>::x << endl; // Output 1
    return 0;
}


It is is functional but seems heavy-handed.

Thanks,
Rhys
RhysU
Registered Member
Posts
5
Karma
0
Hmmm... I take back the functional comment. This slight variant
Code: Select all
#include <iostream>
#include <Eigen/Core>
#include <boost/array.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_base_of.hpp>

using namespace std;

template<class T, class Enable = void>
struct value {
    typedef typename T::value_type type;
};

template< class T >
struct value<
    T,
    typename boost::enable_if<
        boost::is_base_of<Eigen::DenseBase<T>, T>
    >::type
> {
    typedef typename T::Scalar type;
};

int main(int argc, char *argv[])
{
    value<boost::array<int,3> >::type x;
    value<Eigen::Vector3i>::type y;
    return 0;
}

spews forth all sorts of errors from Eigen and Boost's combined guts.

This is closer to my use case. I'd like to get access to the STL Container 'value_type' typedef that corresponds to an Eigen type. The same trait should work for an STL Container concept too.

- Rhys


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], q.ignora