Subject: Re: [boost] [integer] Type-safe and bounded integers with compile-time checking
From: Christopher Jefferson (chris_at_[hidden])
Date: 2011-09-06 14:38:24
On 6 Sep 2011, at 19:33, Leif Linderstam wrote:
> Ben Robinson wrote 2011-09-05 21:57:
>> ... I currently call it MetaBoundedInt, and the design philosophy
>> was to provide both an overflow, and ranged checked integer, which leverages
>> as much compile time information as possible to maximize performance, as
>> well as communicate overflow/range errors at compile time when possible.
> That seems to be more in line with how for instance Ada does it, which
> probably means that it is a more reasonable approach than mine. That
> said, part of the reason for me to start looking into this was some
> sloppy use of integers within a project I joined and a fairly strict
> environment would have helped.
>> mbi<throwing_policy, int8_t, -8, 8> var2(0U); // Detects overflow and range
> Why not go all the way and select an underlying int based on the given
>> As you requested Leif, the throwing_policy can be replaced in a release
>> build with an ignore_policy, so that these additional checks can be
>> eliminated for maximum release build performance.
> Then I was not clear enough. If a check cannot be made at compile-time I
> want it to be done at runtime. I do want this type of library to be as
> efficient as possible for better acceptance among developers, but my
> main reason for wanting it to do static checks as much as possible is
> to detect faults as early as possible, and to make it harder to write
> that sloppy code.
Could you give an example of a non-trivial algorithm, where this would help? I am having difficulty imaging something where bounds can be usefully detected at compile time.
Most mathematical algorithms involve ifs and loops, which would would not seem compatible with your technique.
If you want something to work off, consider a simple example like:
int scalar_product(vector<int> v1, vector<int> v2)
int sum = 0;
for(int i = 0; i < v1.size(); ++i)
sum += v1[i] * v2[i];
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk