Re: Ranged Integers
On Sep 3, 8:42 pm, terminator <farid.mehr...@gmail.com> wrote:
On Sep 3, 2:06 am, foothomp...@yahoo.com wrote:
is there a simple integer type library around that behaves like normal
integers, i.e has all the same operations, but except the value can
only be in a specific range, like 0 to 100, and if the value is
outside of this range, it will throw an exception or assert fail?
template<typename Numeric,Numeric low,Numeric up>
struct bounded{
typedef Numeric value_type;
enum{lower=low,upper=up};//no runtime overhead
operator value_type&()const{return value;};
explicit bounded(const Numeric& i);
const value_type& operator=(const Numeric& r);
.....//etc
private:
Numeric value;
};
regards,
FM.
that works with integrals for none-integral numerics(float/double...):
template<typename Numeric,Numeric low,Numeric up>//no memory overhead
for bounds
struct bounded_real{
typedef Numeric value_type;
static const value_type& lower(){
static const value_type data=low;
};
static const value_type& upper(){
static const value_type data=up;
};
...//etc
};