Yes, define a second "getter" template with partial specialization.
template< typename >
struct get_Small_X; // base template is incomplete, invalid
template< typename X > // only specializations exist
struct get_Small_X< Small< X > > {
typedef X type;
};
Now instead of Small<X>::X
you have typename get_Small_X< Small<X> >::type
.
By the way, _X
is a reserved identifier, so you shouldn't use it for anything. X_
is a better choice.
Advanced topic: template introspection.
While I'm thinking about it, you don't need to define this separately for every template. A single master template should do it.
This compiles in Comeau, I know there are rules about matching template template arguments but I think it's OK… template template arguments are forbidden from the master template in partial specialization.
template< typename >
struct get_first_type_argument;
template< template< typename > class T, typename X >
struct get_first_type_argument< T< X > > {
typedef X type;
};
template< typename X >
struct simple;
get_first_type_argument< simple< int > >::type q = 5;
This only works with "unary" templates but could be adapted in C++0x for the general case.