简体   繁体   中英

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

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_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 ).

使用boost的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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM