Boost logo

Boost :

Subject: Re: [boost] [integer] Type-safe and bounded integers with compile-time checking
From: Leif Linderstam (leif.ls_at_[hidden])
Date: 2011-09-06 14:42:18


Mathias Gaunard wrote 2011-09-06 10:30:
> On 05/09/2011 21:27, Leif Linderstam wrote:
>
>> My first go at this was that if the result range cannot be
>> represented by
>> any fundamental int type a compile time error is issued. My latest
>> thoughts though are that the library should instead allow for
>> multi-sized
>> integers, which then of course touches on the current work of
>> Christopher
>> Kormanyos. In the original post I said that the range type should accept
>> types as bounds instead of integers; this is the reason. With types
>> there
>> we can specify really big ranges.
>
> A typed approach prevents iterative programming somewhat, since each
> operation would yield an object of a different type.

I lost you a bit there. The typed approach for ranges is there to
support compile-time constants that are larger than 64 bits. But it is
true that the operators +, -, *, /, %, and even unary - all return an
object of a new type (regardless if the bounds of the range type is
specified with ints or types).

>
> You can't write
> a = 0;
> while(some_cond)
> a += something;
>

Although in my first thoughts of this I did not include +=, -=, etc.,
I now believe that those operators must be supported, otherwise this
kind of library would be too far from normal C++. And indeed, the only
way to implement these is to do range checking at runtime. (I
personally prefer to keep those checks in production code as well, but
that should perhaps be configurable.)

However, this means that the following:

   a += something;

is not the same as:

   a = a + something;

which actually would not compile. Instead it is the same as:

   a = range_conversion<range_of_a>(a + something);

Sincerely,
Leif Linderstam


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