A tag of infinite

infinite is a important concept in programming. the following is a class which implements this concept.

template<typename T> // T was supposed to be legal number types
class neg_infinite
{
public:
    explicit neg_infinite(){}
    bool operator<(T i){return true;}
    bool operator>(T i){return false;}
    friend bool operator<(T i, neg_infinite j){return false;}
    friend bool operator>(T i, neg_infinite j){return true;}
    bool operator==(T i){return false;}
    friend bool operator==(T i, neg_infinite j){return false;}
};
template<typename T>
class pos_infinite
{
public:
    explicit pos_infinite(){}
    bool operator<(T i){return false;}
    bool operator>(T i){return true;}
    friend bool operator<(T i, pos_infinite j){return true;}
    friend bool operator>(T i, pos_infinite j){return false;}
    bool operator==(T i){return false;}
    friend bool operator==(T i, pos_infinite j){return false;}
};

//how to use

neg_infinite<int>() < -999;  // true
pos_infinite<int>() == 999; // false
neg_infinite<int>() == neg_infinite<int>(); // insignificance, compiler-time error

if you want T to be primitive types, you needs to check it by following codes:

template<typename T>
class must_be_primitive_num
{
private:
 must_be_primitive_num(){}
};
template<>class must_be_primitive_num<int>{};
template<>class must_be_primitive_num<long>{};
template<>class must_be_primitive_num<short>{};
template<>class must_be_primitive_num<float>{};
template<>class must_be_primitive_num<double>{};
//...adding others primitive types to specialization

must_be_primitive_num<user_defined_type>(); will rise a compiler-time error.

but notice that the bad news is this tag can not push into a vector<int>, its usage is limited. to allowing this, int must be convert to class infinite. however, consideration of efficiency will prohibit this conversion even implict conversion is convenient.

in fact, a good consideration about programming does not need a concept of infinite because the math in programming language is no longer pure. the distinctions of numbers in math is categorial(R, N...) rather than the field of representation(a little categorial meaning, i.e. int and float). so every numbers used in programming lanuage has its extent, i.e. unsigned int has its range from 0 to 65535(32bit). if you know your code then you also clear about what infinite it is which corresponding to that variable.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值