0

我想做一个非常简单的浮点/双比较函数集,它将比较值到指定的小数点位置。C++模板函数专业化?

#include "stdafx.h" 
#include "CppUnitTest.h" 

#include <exception> 

using namespace Microsoft::VisualStudio::CppUnitTestFramework; 

namespace lqpro 
{ 
    namespace /* anonymous */ 
    { 
    template<typename _type> 
    _type myAbs(_type left, _type right) 
    { 
     throw std::exception("lqpro::myAbs() called with non-float type parameter"); 
    } 

    template<> 
    double myAbs(double left, double right) 
    { 
     return fabs(left - right); 
    } 

    template<> 
    float myAbs(float left, float right) 
    { 
     return fabsf(left - right); 
    } 

    template<typename _type> 
    static _type quick_pow10(int n) 
    { 
     static _type pow10[10] = { 
     1.0, 10.0, 100.0, 1000.0, 10000.0, 
     100000.0, 1000000.0, 10000000.0, 
     100000000.0, 1000000000.0 
     }; 

     return pow10[n]; 
    } 

    } // anonymous... 

    template<typename _type> 
    bool floatCompare(_type left, _type right, const int decimals=5) 
    { 
    _type _mul = quick_pow10<_type>(decimals); 

    _type _left = left * _mul; 
    _type _right = right * _mul; 

    _type _diff = myAbs(left - right); 
    if (static_cast<int>(_diff) == 0) 
     return true; 

    return false; 
    } 

    template<> 
    bool floatCompare<>(float left, float right, const int decimals); 

    template<> 
    bool floatCompare<>(double left, double right, const int decimals); 

} // lqpro... 

namespace lqpro_tests 
{  
    TEST_CLASS(FloatCompare_tests) 
    { 
    public: 

     TEST_METHOD(ComparingFloatsZeroToOneReturnsFalse) 
     { 
     Assert::IsFalse(lqpro::floatCompare(0.0f, 1.0f, 5)); 
     } 

    }; 
} // lqpro_tests... 

我的问题是,这不会为我编译。努力时,得到以下错误...

1>FloatCompare_tests.cpp 
1> Creating library D:\proj\LottoQuestPro\LottoQuestPro_Solution\Debug\LQPro_tests.lib and object D:\proj\LottoQuestPro\LottoQuestPro_Solution\Debug\LQPro_tests.exp 
1>FloatCompare_tests.obj : error LNK2019: unresolved external symbol "bool __cdecl lqpro::floatCompare<float>(float,float,int)" ([email protected]@[email protected]@[email protected]) referenced in function "public: void __thiscall lqpro_tests::FloatCompare_tests::ComparingFloatsZeroToOneReturnsFalse(void)" ([email protected][email protected][email protected]@QAEXXZ) 
1>D:\proj\LottoQuestPro\LottoQuestPro_Solution\Debug\LQPro_tests.dll : fatal error LNK1120: 1 unresolved externals 
1>Done building project "LQPro_tests.vcxproj" -- FAILED. 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

我想我可以只写floatCompare()函数两次,一次为float和一次双,但我不希望重复的代码。 ..

我在做什么错?

感谢

+0

如果参数不是float,则可以使用std :: enable_if停止模板的编译。将以下内容作为模板参数添加到myAbs中:“typename = typename std :: enable_if :: value> :: type”。它在type_traits中。 – Robinson

+0

是否有任何特别的理由抛出异常“调用非浮点型参数”,而不是获取编译时错误,试图调用未知超载?是否有任何特殊的原因使用功能专业化而不是重载?最后,如果删除不必要的未定义的特化,“floatCompare”的工作方式就像你想的那样。 – WindyFields

+0

按myAbs(左 - 右)'你的意思是'myAbs(左,右)' – WindyFields

回答

1

以下声明特(你不要定义

template<> 
bool floatCompare<>(float left, float right, const int decimals); 

template<> 
bool floatCompare<>(double left, double right, const int decimals); 

如果你想明确地实例化它们,它会是

template 
bool floatCompare<float>(float left, float right, const int decimals); 

template 
bool floatCompare<double>(double left, double right, const int decimals); 

但所有使用访问定义,你甚至可以完全忽略茨艾伦e行。

+0

我在想,我必须专门化,否则我必须通过我的代码浮动比较(...)。谢谢 – jump

0

它看起来像你想一些指定的精度范围内比较数字,即有小量比较。这是我用做它的函数:

#include <iostream>  
#include <cmath> 

template <typename T0, 
      std::enable_if_t<std::is_floating_point<T0>::value>* = nullptr> 
bool Equal(T0 a, T0 b, T0 epsilon) 
{  
    return std::abs(a - b) <= epsilon; 
} 

int main() 
{ 
    if (Equal(0.1, 0.15, 0.1)) 
    { 
     std::cout << "They are equal to within 0.1";  
    } 
    else 
    { 
     std::cout << "They are not equal to within 0.1"; 
    } 
}