Live data from Hacker News

Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

bitbucket.org

101–110 of 130 posts

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#102

> The built-in integer types in C++ (int, unsigned, long long, etc.) are mostly unusable because of the lax requirements on bounds. This must be some new definition of the word 'unusable.'

I agree with that statement. How can you use type when you don't know range of values. I want to store year. From 1850 to 2050. What type should I choose? I want to store colour value: from 0 to 2^24-1. What type should I choose?

I've been programming in C and C++ for almost 30 years. This has never been a problem for me.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#103

Earlier quoted context omitted.

How are you envisioning this library being used?

What? I am saying the author did not express the limitation well, and instead made it sound like they don't understand sizeof(int). The explanation that leni536 offered makes a lot more sense, so maybe they should have put it that way instead of using an easily misunderstood term as "built-in integer" to mean "one of several integer types that we may choose based on some ifdef"

Why did you arrive at this confusion?

Why would you assume your observations are relevant to the use case?

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#104

At first glance it seems to be similar to boost::safe_numerics. Are there significant differences? https://github.com/boostorg/safe_numerics

Both libraries do the same thing. Boost lets you set your exception policy and I'm unsure if this Bounded Integer library gives the same level of control.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#105
post #3

> bounded::integer uses built-in integers as the template parameter to determine its bounds. This means that it cannot store an integer larger than an unsigned 64-bit or unsigned 128-bit integer (depending on the platform) or smaller than a signed 64-bit or signed 128-bit integer. This restriction should be removed at some point in the future. So when an overflow happens here I assume it's a compile time error (UB du…

I am not aware of any compiler or platform where int is 128 bits. The most common these days, even on 64 bit platforms, is 32 bits, which they don't even mention. I am not sure why they chose to speculate about sizeof(int) in this way, vs. expressing the limit in terms of something more concrete.

Intel supports them depending on the processor.

SSE2 has native 128-bit math, AVX has 256-bit native integer math, and AVX-512 has 512-bit integer math.

See https://en.wikipedia.org/wiki/AVX-512

The Intel compiler supports these types.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#106
post #83

Earlier quoted context omitted.

It does not change your existing integers or compilation time. Only where you use the new types the compiler will have more work to do. What would be the alternative in those places? Manual code? Preprocessor macros? Assertions everywhere? A theorem prover? To me the implementation seems to be a good solution given that you don't want to change the core language.

> What would be the alternative in those places? Fix the damn language.

As in, adding more features to C++? And then people cry about how complex C++ is.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#107
post #30

At this point, when I see libraries like this all I can think is ”oh good, my compile times aren’t long enough, lets make EVERY INTEGER a template”. Also, while I can reasonably beliveve that most of the overhead goes away at -O2 or -O3, this has to just trash performance for debug builds, which is not unimportant.

In my line of software work, I needed variable-precision integers (think uint5_t, uint13_t, etc) to express details of the architecture cleanly and avoid lots of manual bit-masking, which naturally meant having to use templates.

I found that it didn't really hurt compile times much (500KLOC compiles in about 40 seconds with make -j on a 16-core box), and performance is just barely impacted at -O2, but it definitely destroys debug build performance completely and makes a mess of code profiling and backtrace debugging output.

There are also all kinds of subtle gotchas: you can make "x ? y : z" work when y and z are different primitive types, but not if one is a template. You cannot make them work with printf("%d", x) directly (it compiles fine and then you run into problems), etc.

Trying to enforce stronger type checking (not allowing the implicit mixing of signed and unsigned, etc) introduces endless worlds of pain and ambiguous operator overload problems.

Trying to make eg "x++" or "x+y" return another template instance instead of a primitive type similarly causes a world of pain everywhere.

I still feel it was 100% worth it in my case, but it has many pitfalls. Taking it even further to bounding to specific ranges as this library aims to do feels even more fraught with danger. I do not envy the work of its authors.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#108
post #83

Earlier quoted context omitted.

> What would be the alternative in those places? Fix the damn language.

As in, adding more features to C++? And then people cry about how complex C++ is.

Stop worrying about 9-bit bytes, only logical shift right for signed integers, ones-complement and sign-bit integers, etc and define this stuff in the standard. PDP-11 was a long time ago. C++20 is heading in the right direction with defining twos-complement. The next good step would be declaring signed overflow as wrapping (eg -fwrapv)

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#109

Earlier quoted context omitted.

What? I am saying the author did not express the limitation well, and instead made it sound like they don't understand sizeof(int). The explanation that leni536 offered makes a lot more sense, so maybe they should have put it that way instead of using an easily misunderstood term as "built-in integer" to mean "one of several integer types that we may choose based on some ifdef"

Why did you arrive at this confusion? Why would you assume your observations are relevant to the use case?

Did you read this part:

> bounded::integer uses built-in integers as the template parameter to determine its bounds. This means that it cannot store an integer larger than ...

They say the limitation arises from using "built-in integers" as template parameters. It is reasonable to assume "built-in integer" means "int". It is more a stretch to assume "built-in integer" means any number of types depending on the evaluation of several ifdefs [which is what I found in the source].

There is nothing about a "use case" relevant to any of that quoted statement, so I find your reply very confusing too.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#110
post #95
post #89

Earlier quoted context omitted.

It was indeed copied, by Pascal, Turbo Pascal, Delphi, Modula-2, Modula-3, unfortunately not by the languages that won.

Copied from Pascal to Ada, no?

Indeed, just making the point it didn't came and stuck with Ada alone.

I should have taken the effort to write it better, sorry about misinformation.

Post reply on HN