3 ms·
> C Printf never was, never could be and never will be a suitable way to output data from C++. Now excuse me while I go through the list of thousands of predefi
by jstimpfle 3y ago
> C Printf never was, never could be and never will be a suitable way to output data from C++. Now excuse me while I go through the list of thousands of predefined format macros to find out which I need to use to output a uint_fast16_t without making the compiler vomit nonsense.
printf("%d\n", (int) myfast16_t);
Not that terrible for a type that I've never used, nor seen used.
- josefx 3y agouint_fast16_t is the fastest type (for some undefined meanings of fast) that holds at least 16 bits, I don't think the standard guarantees that it even has to fit into an int, so we are already silently discarding data. Worse if it uses an unsigned int then you are casting from unsigned to signed with a potential undefined singed overflow involved.
- jstimpfle 3y agoI would have assumed that you don't want to use uint_fast16_t to store anything that doesn't fit in 16 bits. If you cast to signed that can't represent the complete range that still wouldn't be called "overflow" AFAIK, and no matter what you call it it is not "undefined". And assuming 32-bit ints there is no loss of information given a 16-bit ints. You can also just cast to unsigned or whatever type you think is enough (you should know). The point is, use a conversion, cast to a simple type, make your code compatible.
- josefx 3y ago> I would have assumed that you don't want to use uint_fast16_t to store anything that doesn't fit in 16 bits. I could have a 16 bit wide bitmask in it, lets flip them ~myMaskFast16, that leaves the higher order bits set to 0xFF... whether I care about them or not. > The point is, use a conversion, cast to a simple type, make your code compatible. Casting to a type that depending on platform may or may not hold enough space to represent the value is not "making it compatible" it makes it non portable.
- IAmLiterallyAB 3y ago> assuming 32 bit ints That's really not a valid assumption. There are 16 bit int platforms, I've worked on them. It's not even that uncommon. Casting in this scenario is simply incorrect. The correct thing to do is to use the formatting macros e.g. PRIuFAST16. Which noone ever does because it's gross and most developers don't actually care about portability.