Well done. It's an infinite loop.
As can be seen from
is_integral, the
unsigned char type is treated as an integral type.
The trick here relates to the 1 byte range of
unsigned char, which can hold values from 0..255 inclusive, as well as
implicit conversions done using the
integral promotion rules.
In the expression
2 * half_limit, the
2 is of type
int, which is larger than
unsigned char, hence the
half_limit value is promoted from
unsigned char to
int before doing the multiplication. The result is then
2 * 150 = 300, and the type is
int.
Meanwhile, the loop variable is still an
unsigned char, with a range limited to 0..255 inclusive. As such, it can never reach the loop termination condition at
300. For
unsigned types, there is well defined wrap around when it overflows the range. Hence the counter will reset to
0 once it overflows the maximum range. The result being an infinite loop, forever counting from
0 to
255.
Side note:
signed types do not have well defined wrap around semantics according to the C++ standard. There is no guarantee about what will happen if you overflow a signed type. In practice, you can usually expect to see identical wrap around with
signed types, simply because that's convenient and natural for compilers to allow, given the typical behaviour of CPU instructions on most architectures (such as 2's complement math). If a C++ compiler was targeting a non 2's-complement architecture, such as one that used a sign bit, you might get different results when a
signed value overflows.