News for hackers. Stuff that matters.
Re:Bounded Integer: Header-only C++ library replaces integer... by vbezhenar on Sunday September 29, 2019 @10:55AM (#21107366)
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?
Re:Bounded Integer: Header-only C++ library replaces integer... by billsnow on Sunday September 29, 2019 @12:05PM (#21107806)
How long does it take you to code a program if you are being bogged down by such simple problems? Use ints and do bounds checking when it's necessary to do so. Is every single bucket of information a reinvention of types?
Re:Bounded Integer: Header-only C++ library replaces integer... by ori_b on Sunday September 29, 2019 @11:01AM (#21107397)
int, long. The standard specifies their minimum sizes.
Re:Bounded Integer: Header-only C++ library replaces integer... by josefx on Sunday September 29, 2019 @11:35AM (#21107610)
Or include cstdint and use the fast or least types as needed.
Re:Bounded Integer: Header-only C++ library replaces integer... by brandon_weekly on Sunday September 29, 2019 @11:27AM (#21107570)
It specifies the minimum sizes, in the sense that sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) etc... but no requirements on how big they actually are. You could (in theory) end up with all of those being 9 bits wide on some weird platform.
Re:Bounded Integer: Header-only C++ library replaces integer... by tspiteri on Sunday September 29, 2019 @01:49PM (#21108470)
No, they cannot all be 9 bits, they have higher minimums.

https://en.wikipedia.org/wiki/C_data_types#Basic_types

Re:Bounded Integer: Header-only C++ library replaces integer... by anilakar on Sunday September 29, 2019 @11:10AM (#21107458)
X86 has also had saturation arithmetic since MMX was introduced.
Re:Bounded Integer: Header-only C++ library replaces integer... by pjc50 on Sunday September 29, 2019 @11:25AM (#21107556)
.... However, unless you use non portable intrinsics, you can't make the compiler use MMX. So in practice you get wrap on overflow.

Worse, the compiler is entitled to use saturation arithmetic if you've assumed wrapping.

Re:Bounded Integer: Header-only C++ library replaces integer... by jstanley on Sunday September 29, 2019 @12:06PM (#21107812)
> Worse, the compiler is entitled to use saturation arithmetic if you've assumed wrapping.

C89 (draft[1], because the actual spec is not public) section 3.1.2.5 states:

> ... a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.

So, unless I'm misunderstanding, the compiler is not entitled to use saturation arithmetic on unsigned integers.

[1] http://port70.net/~nsz/c/c89/c89-draft.html

Re:Bounded Integer: Header-only C++ library replaces integer... by ncmncm on Sunday September 29, 2019 @03:22PM (#21109106)
signed and unsigned have different semantics.

The compiler is allowed to do literally anything on signed overflow, including launching the missiles, and not running the program. Or both.

Re:Bounded Integer: Header-only C++ library replaces integer... by gpderetta on Sunday September 29, 2019 @11:52AM (#21107714)
You can build portable libraries on top of nonportable primitives.
Re:Bounded Integer: Header-only C++ library replaces integer... by leni536 on Sunday September 29, 2019 @11:13AM (#21107486)
You should never rely on the underlying CPU architecture's behavior on integer overflow when you write C or C++. It is undefined and not unspecified or implementation defined.
Re:Bounded Integer: Header-only C++ library replaces integer... by tom_ on Sunday September 29, 2019 @02:18PM (#21108676)
Undefined behaviour is undefined only by the standard - an implementation may choose to handle undefined behaviour in some documented, specific fashion. Undefined behaviour exists to give implementations exactly this flexibility.
Re:Bounded Integer: Header-only C++ library replaces integer... by leni536 on Sunday September 29, 2019 @04:37PM (#21109471)
> an implementation may choose to handle undefined behaviour in some documented, specific fashion

Or they can leave it undefined and not make any guarantees. I do not think that any compilers on any platform makes any guarantee on signed overflow.

Re:Bounded Integer: Header-only C++ library replaces integer... by leetcrew on Sunday September 29, 2019 @12:08PM (#21107830)
true, but the behavior of unsigned integers is defined such that they can't overflow. the "wrapping" behavior of unsigned ints is defined: you get the arithmetic result modulo the max value for that width.
Re:Bounded Integer: Header-only C++ library replaces integer... by andai on Sunday September 29, 2019 @11:39AM (#21107652)
You can store year 1850-2050 in a byte.
Re:Bounded Integer: Header-only C++ library replaces integer... by icedchai on Sunday September 29, 2019 @09:21PM (#21110943)
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 integer... by golemotron on Sunday September 29, 2019 @01:27PM (#21108321)
> I agree with that statement. How can you use type when you don't know range of values.

That might be a question for developers of the billons of lines of C++ in production. Asking them, you'd discover how they they actually use the builtin types.