
Dan McLeran wrote:
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
Just a small proposal. It would be nice if CheckedIntegralValue would chose the actual type of value itself. For example, for range -10 .. 10 it would use signed char, for -1000 .. 1000 signed int, and for 0 .. 300 unsigned int. Of course, its type detecting logic should be overrideable. I think, it would look something like: template< signed long long min, signed long long max, typename T = typename auto_detected< min, max >::type, typename BoundingPolicyT = throw_on_overflow > struct CheckedIntegralValue;