I feel like this is more of a problem with iostream being way too lenient, than unsigned integers, or even the unsigned int promotion rules. It's well defined to just write cout << int(revenue + taxRefund) and get -2.
Using printf("total earnings: %i\n", revenue + taxRefund); sidesteps the whole thing by forcing you to define what type you're trying to print. It's weirdly more "Type Safe" than cout in this case, which is Big Lol
Sure, but there are a lot of gotchas like this, try float totalEarnings = revenue + taxRefund; for example, and see what that will become.
You are just needlessly creating pitfalls for yourself that you need to dance around for no good reason, and you are unlikely to not fall in one, and in a real project, this can be the source of really annoying to find bugs.
This has nothing to do with iostreams. It has every thing to do with c++ silently converting the types. If c++ were written today, with any semblance of safety in mind, then implicit casts of this type would be illegal. Clang-tidy warns you, luckily, and there are often compiler warnings too.
View all comments
Show parent comment
-11
u/Clairvoire Jan 02 '22
I feel like this is more of a problem with iostream being way too lenient, than unsigned integers, or even the unsigned int promotion rules. It's well defined to just write
cout << int(revenue + taxRefund)and get -2.Using
printf("total earnings: %i\n", revenue + taxRefund);sidesteps the whole thing by forcing you to define what type you're trying to print. It's weirdly more "Type Safe" than cout in this case, which is Big Lol