4 ms·
The usual arithmetic conversions linked from the article [0] are a bit different from the ones I know. In particular, if int can represent all values of all ope
by lultimouomo 4y ago
The usual arithmetic conversions linked from the article [0] are a bit different from the ones I know.
In particular, if int can represent all values of all operands, both of them will be converted to int.
Thus comparing unsigned short and short will usually (if short is narrower than int) do the right thing. C++Insights (which I didn't know! This is the real pearl of this article.) agrees with me [1].
[0] https://en.cppreference.com/w/cpp/language/operator_arithmetic#Conversions https://en.cppreference.com/w/cpp/language/operator_arithmet...
[1] https://cppinsights.io/lnk?code=I2luY2x1ZGUgPGNzdGRpbz4KCmludCBtYWluKCkKewogICAgc2hvcnQgYSA9IC0xOwogICAgdW5zaWduZWQgc2hvcnQgYiA9IDE7CiAgCiAgcHJpbnRmKCIlZCIsIGEgPCBiKTsKfQ==&insightsOptions=cpp17&std=cpp17&rev=1.0 https://cppinsights.io/lnk?code=I2luY2x1ZGUgPGNzdGRpbz4KCmlu...
- anonymoushn 4y agoThe relevant part of your first link seems to be right at the beginning: > If the operand passed to an arithmetic operator is integral or unscoped enumeration type, then before any other action (but after lvalue-to-rvalue conversion, if applicable), the operand undergoes integral promotion. Clicking "integral promotion" leads here: https://en.cppreference.com/w/cpp/language/implicit_conversion#Integral_promotion https://en.cppreference.com/w/cpp/language/implicit_conversi...