64,646
社区成员
发帖
与我相关
我的任务
分享
template<class T>
class Compare
{
public:
static bool IsEqual(const T& lh, const T& rh)
{
return lh == rh;
}
};
class Compare
{
public:
template<typename T>
bool static IsEqual(T const& a, T const& b)
{
return a == b;
}
static bool IsEqual(double a , double b)
{
return a >= b && b <= a;
}
static bool IsEqual(float a, float b)
{
return a >= b && b <= a;
}
};
bool b ;
b = Compare::IsEqual(1, 2); //generic veriosn
b = Compare::IsEqual('a', 'b'); //generic veriosn
b = Compare::IsEqual(1.0f, 2.0f); //float veriosn
b = Compare::IsEqual(1.0, 2.0); //double veriosn
struct Compare
{
template<class T>
static bool IsEqual(const T& t1, const typename std::enable_if<std::is_floating_point<T>::value, T>::type & t2)
{
cout << "t1=" << t1 << "\tt2=" << t2 << endl;
return t1 == t2;
}
template<class T>
static bool IsEqual2(const T& t1, const T& t2)
{
static_assert(is_floating_point<T>::value, "floating type unsupported.");
cout << "t1=" << t1 << "\tt2=" << t2 << endl;
return t1 == t2;
}
template <class T>
static bool IsEqual3(const T&t1, const T& t2);
private:
template <class T>
static bool isEqual_Impl(const T& t1, const T& t2)
{
return t1 == t2;
}
};
template <>
static bool Compare::IsEqual3<float>(const float&t1, const float& t2)
{
return isEqual_Impl<float>(t1, t2);
}
template <>
static bool Compare::IsEqual3<double>(const double&t1, const double& t2)
{
return isEqual_Impl<double>(t1, t2);
}
template <>
static bool Compare::IsEqual3<long double>(const long double&t1, const long double& t2)
{
return isEqual_Impl<long double>(t1, t2);
}
template <typename T> struct tag;
template<> struct tag<float>{};
template<> struct tag<double>{};
template<> struct tag<long double>{};
template <typename T>
struct Compare4 : tag<T>
{
static bool IsEqual( const T& t1, const T& t2)
{
return t1 == t2;
}
};
// 多一个参数
template <typename T, typename Enable = void>
struct Compare5; // not implemented
template<class T>
struct Compare5<T, typename enable_if<is_floating_point<T>::value >::type> {
static bool IsEqual(const T& t1, const T& t2)
{
return t1 == t2;
}
};
void test()
{
const float& x = 1;
float y = 2;
Compare::IsEqual(1.0, 1.0);
//Compare::IsEqual(1, 1); // complie error
Compare::IsEqual(1.0, 1); // allowed
Compare::IsEqual(x, y);
Compare::IsEqual2(1.0, 1.0);
//Compare::IsEqual2(1, 1); // compile error
//Compare::IsEqual2(1.0, 1); // not allowed
Compare::IsEqual2(x, y);
Compare::IsEqual3(1.0, 1.0);
//Compare::IsEqual3(1, 1); // complie error
//Compare::IsEqual3(1.0, 1); // not allowed
Compare::IsEqual3(x, y);
Compare4<float>::IsEqual(1.0, 1.0);
//Compare4<int>::IsEqual(1, 1); // complie error
Compare4<float>::IsEqual(1.0, 1); // allowed
Compare4<float>::IsEqual(x, y);
Compare5<float>::IsEqual(1.0, 1.0);
//Compare5<int>::IsEqual(1, 1); // complie error
Compare5<float>::IsEqual(1.0, 1); // allowed
Compare5<float>::IsEqual(x, y);
}
template <typename T> struct tag;
template struct tag<float>{};
template struct tag<double>{};
template <typename T>
struct compare : tag<T>
{
static bool is_equal( const T lhs, const T rhs );
};
struct Compare
{
template<class T>
static bool IsEqual(T t, typename std::enable_if<std::is_floating_point<T>::value, T>::type)
{
return true;
}
};
Compare::IsEqual(3, 2); // compile error
Compare::IsEqual(3.0, 2.0); // compile ok
template class Compare<float>;
template class Compare<double>;
#include <iostream>
using namespace std;
template<class T>
struct Compare {
static_assert((is_same<T, float>::value)
|| (is_same<T, double>::value)
, "not float or double");
};
int main() {
Compare<float> c;
Compare<int> c;
cout << sizeof(c) << endl;
}
#include <boost/mpl/assert.hpp>
#include <boost/type_traits/is_same.hpp>
template<class T>
class Compare
{
public:
static bool IsEqual(const T& lh, const T& rh)
{
BOOST_MPL_ASSERT_MSG((boost::is_same<T, double>::value || boost::is_same<T, int>::value), type_must_be_int_or_double, (T));
return lh == rh;
}
};
不过, boost 实现这些也是依赖于特化的.
当然也可以自己封装类似的东西而不引入 boost