Distinguishing integer from floating point types in a template

前端 未结 3 1165
逝去的感伤
逝去的感伤 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 13:52

    You can specialize on

    Then you can group functions based on categories

    template class isEqualbyType;
    
    template
    class isEqualbyType
    {
    public:
        static bool cmp( T a, T b ) { 
            return a == b; }
    };
    
    template
    class isEqualbyType
    {
    public:
    
        static bool cmp( T a, T b ) {
            static const double epsilon=1e-50;
            return abs( a - b ) < epsilon; }
    };
    
    template
    bool isEqual( T a, T b )
    {
       return isEqualbyType::value>::cmp(a,b);
    };
    

提交回复
热议问题