Is there a way to compile-time assert if a variable is a class, struct or a basic type in c++?

StackOverflow https://stackoverflow.com/questions/828442

  •  06-07-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

is_class is a member of the Boost.Type_Traits library. The implementation is probably similar to what you already have. You would use in in conjunction with enable_if to create the function if appropriate:

template <class T>
   typename enable_if_c<boost::is_class<T>::value>::type 
     foo() {  }

Or the equivalent:

template <class T>
   typename enable_if<boost::is_class<T>>::type 
     foo() {  }

The function foo is only generated if the type T is of class type.

The return value for the function, if it is generated, is the second parameter (omitted) to the enable_if template (the default is void).

OTHER TIPS

Use boost's type traits.

If it's only asserting during compile time, you could use BOOST_STATIC_ASSERT and Boost.TypeTraits to check if types are suitable. If you want to enable a template for specific types, you may have success using Boost's enable_if.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top