Hi,
I am trying to figure out how to generically wrap some similar classes from an external library, possibly using traits classes.
The library exposes two classes, say X and Y, and several related classes, say vec_X, vec_Y, mat_X, mat_Y, where vec_X is a vector of X's, mat_X is a matrix of X's, etc. The interfaces for X and Y are fairly regular, and I would like to write some generic code that can operate on objects of type either X or Y.
Suppose for example I have a function that takes either X or Y as input, and needs to operate internally with vec_X/vec_Y and mat_X/mat_Y. The simple solution is to do something like this:
1 2 3 4 5 6 7 8
|
template<typename val_t, typename vec_t, typename mat_t>
void my_func(const val_t& input)
{
vec_t my_vec;
mat_t my_mat;
...
}
|
To operate on X's I would invoke like this:
|
my_func<X, vec_X, mat_X>(...)
|
This works, but it becomes quite unreadable with all the type parameters written down every time. (In the example above there is only vec and mat; in my actual situation I have many more to deal with.)
I tried to solve this using traits classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
// traits class
template<typename T>
struct my_traits { };
// explicit specializations
template<>
struct my_traits<X>
{
typedef X val_t;
typedef vec_X vec_t;
typedef mat_X mat_t;
};
template<>
struct my_traits<Y>
{
typedef Y val_t;
typedef vec_Y vec_t;
typedef mat_Y mat_t;
};
// function I am interested in
template<typename T>
void my_func(const my_traits<T>::val_t& input)
{
my_traits<T>::vec_t my_vec;
my_traits<T>::mat_t my_mat;
...
}
|
But this doesn't work, gcc barfs on it. The problem seems to be that the compiler doesn't know enough about "my_traits<T>" at compilation time to be able to allow a function definition with a parameter of type "my_traits<T>::val_t".
Is there some way to do what I'm trying to do? Or do I have to put up with writing out the template parameters every time?
thanks in advance,
david