Boost logo

Boost :

From: Stephen Gross (sgross_at_[hidden])
Date: 2005-09-14 12:19:01


> I've written a template class to hold a range of valid integral values. My
> intent was to mimic Ada's ability to define a type like this:
>
> type SmallInt is range -10 .. 10;
>
> One can then declare objects of this type and any subsequent assignment
> that violated this range constraint woud throw an exception.
>
> I have built a C++ template class that does the same thing:
>
> template<typename T, T min, T max>
> struct CheckedIntegralValue
>
> To define a type that can hold the same range as the example above:
>
> typedef CheckedIntegralValue<int, -10, 10> SmallIntType;
>
> SmallIntType i = -10;//OK
> SmallIntType i2 = -100;//Will throw an exception at run-time for value
> out-of-range
>
> I won't include the whole thing here, but I can do so if there is enough
> interest. I have defined most of the operators one needs to use this type
> just as one would use a 'normal' integer.
>
> Would anyone be interested in something like this in the Boost libraries?

Since the assignment check is done at runtime (as it has to be), why make
the range restriction at compile time? Isn't it more useful to have a class
that works more like this:

=== CODE ===
CheckedIntegralValue x;
x.setLowerLimit(-5);
x.setUpperLimit(1000);
x = 10;
x = 9000; // Fails at runtime
===========

Does this make sense?

--Steve


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk