小编jor*_*rge的帖子

c ++模板运算符重载std :: enable_if

我在编译此模板时遇到问题.我只想创建一个运算符,其功能取决于数据类型(int或float).我不是专家,所以我将不胜感激.

template <class T> struct eq {
  typename std::enable_if<std::is_floating_point<T>::value, T>::type
  operator() (const T& x, const T& y) const {
    T* paux;
    if(sizeof(T) == 4){ //float
      uint32_t val = 0;
      if(x == y){
    val = ~val;
    T* paux = reinterpret_cast<T*>(&val);
    return *paux;
      }
      else{
    T* paux = reinterpret_cast<T*>(&val);
    return *paux;
      }
    }
    else{ //double
      uint64_t val = 0;
      if(x == y){
    val = ~val;
    T* paux = reinterpret_cast<T*>(&val);
    return *paux;
      }
      else{
        T* paux = reinterpret_cast<T*>(&val);
        return *paux;
      }
    }
  } …
Run Code Online (Sandbox Code Playgroud)

c++ templates compiler-errors

2
推荐指数
1
解决办法
513
查看次数

标签 统计

c++ ×1

compiler-errors ×1

templates ×1