mardi 4 février 2020

define a common template c++ class for arithmetic types and pointers

I need to define a common template class for arithmetic types and pointer types.

following is the code I tried but I never got it correct. I need to implement it using g++4.4.7, because of that I am using boost.

//primary template class
template <class T, class Enable = void>
struct Class
{
};


template <class C>
struct Class<C, typename boost::enable_if_c<boost::is_arithmetic<C>::value || boost::is_pointer<C>::value>::type>
{
  static inline typename boost::enable_if_c<boost::is_arithmetic<C>::value, void>::type
  print(const C& obj)
  {
    std::cout << "ARITHMETIC TYPE" << std::endl;
  }

  static inline typename boost::enable_if_c<boost::is_pointer<C>::value, void>::type
  print(const C& obj)
  {
    Class<uint64_t>::print(reinterpret_cast<const uint64_t&>(obj));
    std::cout << "POINTER" << std::endl;
  }
};

Aucun commentaire:

Enregistrer un commentaire