3 ms·
Actually, what will happen according to the C++ standard depends on the size of int. C++ is doing implicit integer promotion of integer variables with types sm
by dannymi 4y ago
Actually, what will happen according to the C++ standard depends on the size of int.
C++ is doing implicit integer promotion of integer variables with types smaller than int, and that promotion converts those values (of the operands of the +) to int (gross generalization yeah yeah).
So the result on g++ amd64 will be 100000 (as you would expect) if int is more than 16 bits (nowadays it is), even WITH `(uint16_t(50000) + uint16_t(50000))`.
I've also tried it in MSVC 2010 and it says the result of `std::cout << (uint16_t(50000) + uint16_t(50000)) << std::endl` is 100000 (both on win32 and on x64).
Try it on an arduino and you will get 34464 (with g++ targeting 8 bit atmel).
Think you want implicit integer promotion in a systems language? You really don't. They are an unnecessary language feature.
Also, the article is only tangentially about that--that's just an intro. The actual body makes very good points, and I think it's more than a little tongue-in-cheek :)
- cynwoody 4y agoApparently, as the GP points out, the article author meant to write asterisk where he wrote plus. uint16(50_000) * uint16(50_000) is uint32(2_500_000_000), which turns out to be int32(-1_794_967_296), the garbage result the author cites.