As an alternative to declaring a traits class using SFINAE, you can use it more subtly with partial specialization.
template< typename T >
struct empty { // support class is like stripped-down enable_if
typedef void type;
};
template< class T, typename v = void > // v is always void!
struct element {
typedef typename T::value_type type;
};
template< class T, typename v >
struct element< T const, v > {
typedef typename T::value_type const type;
};
template< class T > // T in deduced context, T::element_type is SFINAE:
struct element< T, typename empty< typename T::element_type >::type > {
typedef typename T::element_type type;
};
… you might want to add another case to make element_type
const for const T
? Unfortunately this doesn't work in GCC, although Comeau accepts it.
template< class T >
struct element< T const, typename empty< typename T::element_type >::type > {
typedef typename T::element_type const type;
};
Code I used to test this:
struct has_et {
typedef int element_type;
};
struct has_vt {
typedef char value_type;
};
char c;
int i;
element<has_vt>::type *cp = &c;
element<has_et>::type *ip = &i;