Distinguishing integer from floating point types in a template

前端 未结 3 1173
逝去的感伤
逝去的感伤 2021-02-09 13:27

I\'d like to perform similar, but not identical computations for several integer types (16, 32, 64 bits) and floating point types (float, double, long double). Most of the code

3条回答
  •  悲哀的现实
    2021-02-09 14:08

    Yes, you can use SFINAE in combination with the metafunctions from

    #include
    
    template
    typename std::enable_if<
        std::is_integral::value,
        bool>::type
    
    isEqual(IntegralType a,IntegralType b)
    {
        return a == b;
    }
    
    template
    typename std::enable_if<
        std::is_floating_point::value,
        bool>::type
    
    isEqual(FloatingType a,FloatingType b)
    {
        return fabs(a-b) <  std::numeric_limits::epsilon();
    }
    

提交回复
热议问题