64,643
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
template <int N=1>
class Compare
{
public:
template <typename T>
inline bool operator()(const T& a,const T& b)const;
};
template<int N>
template <>//此部分特化函数如何在类外定义 或 应如何申明特化?
inline bool Compare<N>::operator()(const double& a,const double& b)const
{
if(N > 0) return a - b > 1E-7;
if(N < 0) return a - b < -1E-7;
return (a - b >-1E-7) && (a - b<1E-7) ;
}
int main()
{
double a=18.02; double b=18.0;
if(Compare<>()(a,b))
std::cout<<"a > b"<<std::endl;
return 0;
}
template <int N>
class Compare
{
public:
template <typename T>
bool operator()(const T& a,const T& b)const
{
if(N > 0) return a > b;
if(N < 0) return a < b;
return a == b;
}
template<>
bool operator()<double>(const double &a,const double &b)const//<double>放错地方了,还是?也试过不同位置,都不行
{
if(N > 0) return a - b > 1E-7;
if(N < 0) return a - b < -1E-7;
return (a - b >-1E-7) && (a - b<1E-7) ;
}
};