From: Dan McLeran (dan.mcleran_at_[hidden])
Date: 2005-09-15 11:28:26
I actually have a run-time version of this as well, but I prefer to use the
compile-time version wherever I can for the reasons posted by Marciej
"Stephen Gross" <sgross_at_[hidden]> wrote in message
>> 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
>> 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 = 10;
> x = 9000; // Fails at runtime
> Does this make sense?
> Unsubscribe & other changes:
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk