I want to write a trait that returns the integral type (float, int, char...) of a given type. Base is:
template< class T, typename T_SFINAE = void >
struct IntegralType;
template< class T >
struct IntegralType< T, std::enable_if< (std::is_integral<T>::value || std::is_floating_point<T>::value) > >{
using type = T;
}
template< class T >
struct IntegralType<T>: IntegralType<T::type>{}
And I want it to return double for:
struct foo{
using type = double;
}
struct bar{
using type = foo;
}
IntegralType<double>::type == double
IntegralType<foo>::type == double
IntegralType<bar>::type == double
This does not work. I have to merge the first and 2nd declaration like that:
template< typename T, bool isIntegral = (std::is_integral<T>::value || std::is_floating_point<T>::value) >
struct IntegralType{
using type = T;
};
template< typename T >
struct IntegralType< T, false >: IntegralType< typename T::type >{};
But what now, if a user of my library has types with members named "MyType" instead of "type"? How could I make it possible to specialize this on structs like:
struct foobar{
using MyType = double;
}
Is this even possible? Actually looks like it should work with SFINAE
You can do this using
void_t
: