From: Dan McLeran (dan.mcleran_at_[hidden])
Date: 2005-09-15 15:32:45
Actually, I hadn't thought about that possibility. How would one implement
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>
typedef typename CheckedIntegralValue<T1::min + T2::min, T1::max +
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_at_[hidden]> wrote in message
> 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
> /michael toksvig
> "Dan McLeran" <dan.mcleran_at_[hidden]> wrote in message
>>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
>> 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_at_[hidden]> wrote in message
>>> Dan McLeran wrote:
>>>> I've written a template class to hold a range of valid integral values.
>>>> 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
>>>> 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
>>> 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:
>> Unsubscribe & other changes:
> 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