Boost logo

Boost :

Subject: Re: [boost] a safe integer library
From: Robert Ramey (ramey_at_[hidden])
Date: 2016-01-12 14:05:23


On 1/12/16 8:46 AM, Brook Milligan wrote:
>> On Dec 17, 2015, at 12:16 AM, David Stone <david_at_[hidden] <mailto:david_at_[hidden]>> wrote:
>>
>> I have written a similar library (http://doublewise.net/c++/bounded/ <http://doublewise.net/c++/bounded/>), and
>> I have a few questions about your implementation.
>
> I am curious whether either of you (Robert and David) have handled the need
> to evaluate the condition “is X (an object of type with some
_potentially_ wide range) within bounds of Y (an object of type with
some _potentially_ narrow range)? I have use cases in which I would
like use bounded types, but there are situations in which I need to
capture the result of something like in_range<Y>(x). I am not seeing
this in your libraries but perhaps I am missing something. It does seem
quite feasible to include and would seem to incur no runtime cost under
the same conditions that the construction Y y = {x} would not.
>
> If I am missing this in your libraries, please let me know; otherwise, I throw this out as a suggested addition.

I'm not 100% sure I understand what you mean, but that's not going to
stop me from commenting. The following applies to the safe numerics
library.

all safe types include a closed interval min,max as part of the type. At
compile time, arithmetic operations take this information into account.
  So for example for safe types x and y

x = y

is checked at compile time that the range of x is compared to the range
of y.

if the range of x includes that of y - there is no runtime checking as
is unnecessary.

if the the ranges of x and y do not intersect - there's a static assert.

if the range of x includes the range of y, the operation is performed
with no runtime checking.

The above occurs with all binary operations and compositions thereof.
So it seems that what you want is already incorporated. The public API
doesn't expose the underlying range information. It's not private though.

You should look into the boost - math - interval arithmetic library.
This does the job at runtime and provides more features and more access
the information about the type. It also addresses issues related to
floating point types.

Robert Ramey


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