A tag of infinite
来源:互联网 发布:2015总决赛数据 编辑:程序博客网 时间:2024/05/17 05:11
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.
- A tag of infinite
- A. Infinite Sequence
- A. Infinite Sequence
- Codeforces 622A Infinite Sequence
- CodeForces 622A Infinite Sequence
- CodeForces 622A Infinite Sequence
- CodeForces 622A Infinite Sequence
- CodeForces 622A-Infinite Sequence
- CodeForces 622A Infinite Sequence
- codeforces 675A Infinite Sequence
- CodeForces 675 A Infinite Sequence
- 【CodeForces】675A - Infinite Sequence
- 【CodeForces】[675A]Infinite Sequence
- CodeForces 675 A Infinite Sequence
- CodeForces-622A.Infinite Sequence
- [CodeForces]-675A-Infinite Sequence
- codeforces 675A Infinite Sequence
- POJ_3358 Period of an Infinite Binary Expansion
- HTML代码之Marquee详解
- 程序员,你还能够年轻几岁!!!
- NetAdvantage v6.2中WebGrid中提供删除确认提示
- .net之旅-FileUpLoad(17)
- RedHat Linux 9.0 下访问Windows文件
- A tag of infinite
- 万万不要相信美国人的话
- thirty
- .net之旅-HiddenField (18)
- Developing Application Frameworks in .Net--(Note2, Configuration/Class Factory)
- SCEA介绍、经验和资源
- Plone从无到有教程
- 61条面向对象分析设计原则
- VC编程列举数据库中的表(应用)