I have the following traits class
template<typename T> struct FeatureType;
and I’m using it like so, which works fine:
class Foo { };
template<> struct FeatureType<Foo> {
typedef int value;
};
template<typename T> class Bar { };
template<typename T> struct FeatureType<Bar<T>> {
typedef T value;
};
Is there a way to extent this implementation for generic types to those that have more than one type parameter (unlike Bar above)? The following does not work
template<typename A, typename B> class Huh { };
template<typename A, typename B> struct FeatureType<Huh<A,B>> {
typedef A value;
};
Thanks!
Regular templates
Regular templates do not overload on their template parameters, but you can partially specialize them on arbitrary many template parameters. Your code should work as long as you put
;behind every struct declaration/definition. (note it is a custom to denote nested types inside templates astype, and values asvalue):Output on LiveWorkSpace (gcc 4.7.2)
Note: even if you have multiple formal template parameters (
A,B, or as many as you like), the actual template is partially specialized for the single classHuh<A, B>Variadic templates
If you actually want to have multiple versions of
FeatureTypetaking a different number of template parameters, you need to use variadic templates (C++11)Output on LiveWorkSpace