7 ms·
> 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 defin
by golemotron 7y ago
> 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.'
- LinuXY 7y agoconst std::string unusable = "unsuitable";
- vbezhenar 7y agoI 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?
- ori_b 7y agoint, long. The standard specifies their minimum sizes.
- anilakar 7y agoX86 has also had saturation arithmetic since MMX was introduced.
- leni536 7y agoYou 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.
- leetcrew 7y agotrue, 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.
- tom_ 7y agoUndefined 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.
- leni536 7y ago> 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.
- pjc50 7y ago.... 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.
- gpderetta 7y agoYou can build portable libraries on top of nonportable primitives.
- jstanley 7y ago> 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 http://port70.net/~nsz/c/c89/c89-draft.html
- ncmncm 7y agosigned 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.
- brandon_weekly 7y agoIt 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.
- tspiteri 7y agoNo, they cannot all be 9 bits, they have higher minimums. https://en.wikipedia.org/wiki/C_data_types#Basic_types https://en.wikipedia.org/wiki/C_data_types#Basic_types
- josefx 7y agoOr include cstdint and use the fast or least types as needed.
- andai 7y agoYou can store year 1850-2050 in a byte.
- billsnow 7y agoHow 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?
- golemotron 7y ago> 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.
- icedchai 7y agoI've been programming in C and C++ for almost 30 years. This has never been a problem for me.
- ComputerGuru 7y agoOr “mostly” ;)
- panpanna 7y agoIf you work with formal verification this is a pretty common sight.
- carlmr 7y agoIf 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).
- HelloNurse 7y agoOn 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.
- vips7L 7y agoDoes C++ not have int32_t and the like from C?
- nitwit005 7y agoIt does. I personally stick to those exclusively except for for loop index variables.
- glouwbug 7y agoIt'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
- ncmncm 7y ago"i32" is better.
- vips7L 7y agoWhat would be different about loop variables? I don't really write C++.
- rleigh 7y agoI'm assuming here that they would use the `size_type` for the container they are iterating over. Otherwise I'm not sure.