Of course you could just use good old C printf to get some work done. But if you did that the "real" C++ programmers would sneer at you.
Of course you could just use good old C printf to get some work done. But if you did that the "real" C++ programmers would sneer at you.
The fun thing with printf is that variadic functions cannot handle C++ classes. In the past was doubly fun because compilers proactively compiled an error into the binary instead of raising an error at compile time. So any forgotten c_str() became a crash waiting to happen.
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.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.
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.
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.
As someone who has been programming in C++ since before there were C++ compilers (back in the day, you had to run it through a translator to make it into C code, then use a C compiler), I think I'm as real of a C++ programmer as anybody.
C++ programmers who sneer at you for this are fully worthy of being ignored.
Of course, printf() has its own set of issues as well.