The Usual Arithmetic Confusions -- Shafik Yaghmour
C++’s type conversion rules—particularly the usual arithmetic conversions and integral promotions—are often misunderstood, even by experienced developers. These rules quietly reshape operands behind the scenes during arithmetic and relational operations, sometimes leading to surprising or platform-dependent behavior that can be tricky to debug or reason about.
The Usual Arithmetic Confusions
by Shafik Yaghmour
From the article:
There are a lot of aspects of C++ that are not well understood and lead to all sorts of confusion. The usual arithmetic conversions and the integral promotions are two such aspects. Certain binary operators (arithmetic, relational and spaceship) require their operands to have a common type. The usual arithmetic conversions are the set of steps that gets operands to a common type. While the integral promotions brings integral types smaller than int and unsigned int to either int or unsigned int depending on which one can represent all the values of the source type. This is one of the areas in C++ that comes directly from C, so pretty much all of these examples applies to C as well as C++.
We will see some examples with results that many may find surprising. After seeing some of these cases we will discuss the rules and how they explain each case. While covering each rule we will present examples to clarify the rule. We will referring to ILP32 and LP64 data models and it may help to familiarize yourself with the size of types in these models.
It would also help to understand integer literals and the rules around what the type of an integer literal will be in various cases e.g.
// https://cppinsights.io/s/0ffee264 void f() { auto x1 = 1; // Integer literal 1 will have type int auto x2 = 1U; // Integer literal 1L will have type unsigned int auto x3 = 1L; // Integer literal 1L will have type long int auto x4 = 1UL; // Integer literal 1UL will have type unsigned long int }
I will be including C++ Insights links for many examples.