C++11 定义一个通用模板c++;用于算术类型和指针的类

C++11 定义一个通用模板c++;用于算术类型和指针的类,c++11,templates,boost,template-meta-programming,sfinae,C++11,Templates,Boost,Template Meta Programming,Sfinae,我需要为算术类型和指针类型定义一个通用模板类 下面是我尝试过的代码,但我从来没有得到正确的。我需要使用g++4.4.7实现它,因为我使用的是boost 输出应该是算术的,后跟指针 //primary template class template <class T, class Enable = void> struct Class { }; template <class C> struct Class<C, typename boost::enable_if

我需要为算术类型和指针类型定义一个通用模板类

下面是我尝试过的代码,但我从来没有得到正确的。我需要使用g++4.4.7实现它,因为我使用的是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;
  }
};

int main()
{
  int x = 0;
  Class<int*>::print(&x);
  return 0;
}
//主模板类
模板
结构类
{
};
模板
结构类
{
静态内联typename boost::启用\u if_c::type
打印(常量C&obj)
{

std::cout我将print设置为一个模板函数,它按预期工作

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

  template <class T>
  static inline typename boost::enable_if_c<boost::is_same<C, T>::value && boost::is_pointer<T>::value, void>::type
  print(const T& obj)
  {
    Class<uint64_t>::print(reinterpret_cast<const uint64_t&>(obj));
    std::cout << "POINTER" << std::endl;
  }
};
模板
结构类
{
模板
静态内联typename boost::启用\u if_c::type
打印(常量T&obj)
{

std::cout SFINAE不适用于非模板,如
print
。如果您想使用SFINAE,您必须制作
print
模板。还有其他可能的技术,例如标记分派。@IgorTandetnik谢谢您的提示。我制作了print模板函数,它可以按预期工作。