Boost logo

Boost :

Subject: Re: [boost] a safe integer library
From: Brook Milligan (brook_at_[hidden])
Date: 2016-01-12 14:31:53

On Jan 12, 2016, at 12:05 PM, Robert Ramey <ramey_at_[hidden]> wrote:
> 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 ( <>), 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.

Perhaps I did not ask my question as clearly as I should have. Sorry.

My understanding, which seems to be reinforced by your response, is that all the normal arithmetic and comparison operations involving safe types either complete with mathematically correct results or fail in some way that depends on the policy. Further, this may or may not involve runtime checks depending on the nature of the specific arguments with respect to ranges, etc.

What I am not seeing in the design is a means of inquiring in advance whether or not an operation will succeed without actually performing the operation. This might be relevant for other cases, but I am most interested in making use of the type system, which by design already includes the relevant range information, to facilitate inquiry regarding assignment or construction. Consider the following code:

int x = { /* something possibly large */ };
safe_int<int8_t> y = x; // clearly this requires a run-time check and may fail

Instead, I would like to be able to, for example, branch; something like this:

int x = { /* something possibly large */ };
if (is_convertible<safe_int<int8_t>>(x) {
  do_something_with small values();
} else {

In some cases, e.g., if the positions of the int and safe_int<> were reversed above, no runtime checking is required but the appropriate branch is executed. In others, such as actually illustrated, runtime checking is required just as it would be in an assignment.

In either case, however, the result leverages the type system you have developed to inquire about the relative ranges and values in (I feel) an expressive and compact way.

Is this possible currently? Can it be incorporated given the design of the library? Don’t you already have the internals in place to do something equivalent that could be exposed in fashion like this?

I hope that is clearer. Thanks again.


Boost list run by bdawes at, gregod at, cpdaniel at, john at