If you're gonna go to the trouble of creating a numeric class, why not also include the lower & upper bounds for the number?
I was thinking something like this:
Code:
#include <iostream>
#include <stdexcept>
using namespace std;
template <typename T, T TLowerBound, T TUpperBound>
class Integer
{
public:
Integer()
: m_Num( TLowerBound ) {}
Integer( const T& value )
: m_Num( value )
{
if ( (value < TLowerBound) || (value > TUpperBound) )
{
throw std::out_of_range( "Number is out of range!" );
}
}
T LowerBound() const
{
return TLowerBound;
}
T UpperBound() const
{
return TUpperBound;
}
T Get() const
{
return m_Num;
}
private:
T m_Num;
};
int main()
{
Integer<unsigned short, 0, 10> UShort;
Integer<unsigned int, 1, 200> UInt;
Integer<unsigned long, 3, 3000> ULong;
Integer<short, -1, 40> Short;
Integer<int, -2, 500> Int;
Integer<long, -3, 6000> Long;
cout << "UShort = " << UShort.Get() << endl
<< " Range = [" << UShort.LowerBound() << ", " << UShort.UpperBound() << "]" << endl
<< "UInt = " << UInt.Get() << endl
<< " Range = [" << UInt.LowerBound() << ", " << UInt.UpperBound() << "]" << endl
<< "ULong = " << ULong.Get() << endl
<< " Range = [" << ULong.LowerBound() << ", " << ULong.UpperBound() << "]" << endl
<< "Short = " << Short.Get() << endl
<< " Range = [" << Short.LowerBound() << ", " << Short.UpperBound() << "]" << endl
<< "Int = " << Int.Get() << endl
<< " Range = [" << Int.LowerBound() << ", " << Int.UpperBound() << "]" << endl
<< "Long = " << Long.Get() << endl
<< " Range = [" << Long.LowerBound() << ", " << Long.UpperBound() << "]" << endl;
return 0;
}
I don't have time to add all the operators you did, but you get the idea.
Unfortunately it won't compile for floating point numbers. I think you'd have to pass the Lower/Upper bounds through the constructor for float & double.