
Actually, I hadn't thought about that possibility. How would one implement this? CheckedIntegralValue<int, -10, 100> a = -10; CheckedIntegralValue<int, -100, 20> b = -100; ???????????????????????????????? c = a + b; One could have a template to figure this out but I would leave it up to the developer to specify the type of c. You could do something like this: template<typename T1, typename T2> struct AdditionTypeResolver { typedef typename CheckedIntegralValue<T1::min + T2::min, T1::max + T2::max> type; }; typedef CheckedIntegralValue<int, -10, 100> Type1; typedef CheckedIntegralValue<int, -100, 20> Type2; Type1 a = -10; Type2 b = -100; AdditionTypeResolver<Type1, Type2 >::type c = a + b; "michael toksvig" <michaeltoksvig@yahoo.com> wrote in message news:dgcini$ble$1@sea.gmane.org...
also, the type of a binary operator's return value should combine the intervals of the operands
e.g. given: CheckedIntegralValue<int, -10, 100> a; CheckedIntegralValue<int, -100, 20> b;
then the type of a+b should be CheckedIntegralValue<int, -110, 120>
and given: CheckedIntegralValue<int, -110, 120> sum;
then sum = a+b, sum = a, and sum = b should all compile just fine
conversely, a = sum, b = sum, and a = b should not
this may have been implied, just wanted to make sure
regards,
/michael toksvig
"Dan McLeran" <dan.mcleran@seagate.com> wrote in message news:dga6uu$c6j$1@sea.gmane.org...
I like the idea of having the response to the over/under range condition configurable. Maybe a policy class could handle what to do in that situation? I guess I got a little tunnel vision trying to emulate what Ada does in response to an invalid assignment, which is raise Constraint_Error.
I agree that this does not emulate exactly what is possible in Ada, but I liked the idea of being able to specify that a type can only hold a specific range of values.
"Maciej Sobczak" <prog@msobczak.com> wrote in message news:43289177.9030904@msobczak.com...
Hi,
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.
Careful with that. This approach, in my humble opinion, is very limited. Why do you assume that exception is The Right Thing when the out of bound condition is detected?
These are things that I would like to have, *depending on situation*:
1. throw some exception (this is what you have)
2. abort immediately without doing *anything* (note that exception causes some code to be executed, like destructors of automatic objects - I might want to avoid this and just kill the program immediately)
3. wrap the result (kind of modulo arithmetic, but more general because allowing to have the lower bound different than 0 - say that if the range is 10..20 then 23 should be wrapped to be 13)
4. saturate the result (if you have the range 0..100 and the result of some computation is 150, cut it to 100 and continue with it) - actually, saturated arithmetic is very convenient, for example in signal processing
5. actually allow the out-of-range value, but log that fact somewhere or send me an e-mail, etc.
6. ...
I leave the list open, beacuse it is clear that the library itself should be customizeable in this regard.
More to this - the bare checked class is not really emulating what Ada supports. What about this (Ada):
type SmallInt is range -10 .. 10; subtype EvenSmallerInt is SmallInt range -5 .. 5;
Now, you can use EvenSmallerInt wherever SmallInt is expected, because one is a subtype of another. Ada does not allow multiple supertypes, although that is a logical extension of the idea, quite useful in my opinion and worth to have.
You will find a very sketchy and incomplete implementation of all these ideas at:
<http://www.msobczak.com/prog/bin/safetypes.tar.gz> (3kB)
-- Maciej Sobczak : http://www.msobczak.com/ Programming : http://www.msobczak.com/prog/ _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost