News for hackers. Stuff that matters.
Re:Bounded Integer: Header-only C++ library replaces integer... by golemotron on Sunday September 29, 2019 @10:48AM (#21107324)
> 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.'

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.

Re:Bounded Integer: Header-only C++ library replaces integer... by panpanna on Sunday September 29, 2019 @10:55AM (#21107359)
If you work with formal verification this is a pretty common sight.
Re:Bounded Integer: Header-only C++ library replaces integer... by carlmr on Sunday September 29, 2019 @10:59AM (#21107381)
If you work with smaller embedded systems and you need portability this is also a common sight (when you also quote the part about cstdint, which most embedded people will be happy with).
Re:Bounded Integer: Header-only C++ library replaces integer... by HelloNurse on Sunday September 29, 2019 @12:03PM (#21107792)
On a different platform, these "friendly" types can be larger, typically 2 or 4 times larger which can be devastating for performance, or smaller, leading to overflow. They are only "usable" if you restrict yourself on one version, of one compiler, on one hardware platform.
Re:Bounded Integer: Header-only C++ library replaces integer... by Fronzie on Sunday September 29, 2019 @01:05PM (#21108199)
Since C++11 there are types like 'uint_fast32_t', which does allow for platform independent code.
Re:Bounded Integer: Header-only C++ library replaces integer... by vips7L on Sunday September 29, 2019 @12:18PM (#21107889)
Does C++ not have int32_t and the like from C?
Re:Bounded Integer: Header-only C++ library replaces integer... by nitwit005 on Sunday September 29, 2019 @12:22PM (#21107911)
It does. I personally stick to those exclusively except for for loop index variables.
Re:Bounded Integer: Header-only C++ library replaces integer... by vips7L on Sunday September 29, 2019 @12:45PM (#21108063)
What would be different about loop variables? I don't really write C++.
Re:Bounded Integer: Header-only C++ library replaces integer... by rleigh on Sunday September 29, 2019 @01:39PM (#21108408)
I'm assuming here that they would use the `size_type` for the container they are iterating over. Otherwise I'm not sure.
Re:Bounded Integer: Header-only C++ library replaces integer... by ncmncm on Sunday September 29, 2019 @03:27PM (#21109132)
An unsigned type is sometimes a poor choice for a loop index. The compiler then cannot assume the loop will terminate, and generate worse code. Usually this doesn't matter -- until you discover it did.
Re:Bounded Integer: Header-only C++ library replaces integer... by rleigh on Sunday September 29, 2019 @03:42PM (#21109231)
Good point. Probably not a problem in practice for most containers, but yet another reason for why the STL (and C library, for that matter) should have used a signed size type from the beginning.
Re:Bounded Integer: Header-only C++ library replaces integer... by nitwit005 on Sunday September 29, 2019 @06:10PM (#21109928)
Yep, it's to match vector/array size_type and difference_type, although I tend to just use size_t and ptrdiff_t directly.
Re:Bounded Integer: Header-only C++ library replaces integer... by glouwbug on Sunday September 29, 2019 @12:27PM (#21107945)
It's a good choice. Sometimes your best choice for a general purpose int is

    typedef Int int32_t;
for the entire compilation as a whole, and when things get screwy just bump Int up to int64_t and take the performance hit for the extra bit of safety
Re:Bounded Integer: Header-only C++ library replaces integer... by ncmncm on Sunday September 29, 2019 @03:27PM (#21109140)
"i32" is better.
Re:Bounded Integer: Header-only C++ library replaces integer... by glouwbug on Sunday September 29, 2019 @12:25PM (#21107927)
Yes
Re:Bounded Integer: Header-only C++ library replaces integer... by Ives on Sunday September 29, 2019 @12:21PM (#21107903)
You can still use the Uint32/Uint8/... types. While performance can vary from system to system, the range should be the same on all reasonable platforms.
Re:Bounded Integer: Header-only C++ library replaces integer... by ComputerGuru on Sunday September 29, 2019 @10:54AM (#21107353)
Or “mostly” ;)
Re:Bounded Integer: Header-only C++ library replaces integer... by LinuXY on Sunday September 29, 2019 @10:55AM (#21107364)
const std::string unusable = "unsuitable";