I am trying to implement a template class that would be able to tell me if a variable is a class,structure or a basic type.
So far I’ve come with this:
template< typename T >
class is_class
{
private:
template< typename X >
static char ( &i_class( void(X::*)() ) )[1];
//
template< typename X >
static char ( &i_class( X ) )[2];
public:
static bool const val = sizeof( i_class< T >(0) ) == 1;
};
and ussage:
is_class< int >::val; // false
is_class< some_class_type >::val; // true
The problem is that now I need to write a blank void function in every class that could be used with this code.
Does anyone know a solution to this problem?
is_classis a member of the Boost.Type_Traits library. The implementation is probably similar to what you already have. You would use in in conjunction withenable_ifto create the function if appropriate:Or the equivalent:
The function
foois only generated if the typeTis of class type.The return value for the function, if it is generated, is the second parameter (omitted) to the
enable_iftemplate (the default isvoid).