Boost logo

Boost :

From: Dan McLeran (dan.mcleran_at_[hidden])
Date: 2005-09-14 17:07:25


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_at_[hidden]> wrote in message
news:43289177.9030904_at_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
>


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