Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template specialization for types providing a traits class

Tags:

c++

templates

I have a class

template <typename T> struct Dispatch;

which is used to call a type-specific function. For instance, assume I have dispatchers like

template <> struct Dispatch <MyClass> {
  static void Apply (void* a, MyClass& m)
  {
      ::memcpy (a, &m, sizeof (m));
  }
};

Now I have a bunch of classes for which I have a type-trait, ArrayTypes. I would like to do something like:

 template <> struct Dispatch <enable_if<IsArrayType>>
 {
   template <typename ArrayType>
   static void Apply (void* a, ArrayType& m)
   {
     ::memcpy (a, &m, ArrayTypeTraits<ArrayType>::GetSize (m));
   }
 };

Is this possible?

like image 329
Anteru Avatar asked Jul 02 '11 10:07

Anteru


1 Answers

Use boost enable_if.

If boost is unavailable, check out the enable_if idiom.

like image 149
Kornel Kisielewicz Avatar answered Sep 26 '22 17:09

Kornel Kisielewicz