> What I was getting at is that the only way for int_min <= 'A' <= int_max to be false was a combination of all of those conditions.
You are wrong about the 'only way'.
For
int_min <= 'A' <= int_max
, that char is unsigned is neither a necessary or a sufficient condition.
int_min <= 'A' <= int_max
is guaranteed if and only if the default char is signed.
> You were the one who said he was doing something undefined.
I did not say that 'he was doing something undefined'.
I said that 'there is no guarantee that it will never result in undefined behaviour'.
> I proceed to show how it is undefined...
What you showed and what you believed to be undefined bahaviour is in fact not undefined behaviour.
If the default char is unsigned, and
sizeof(char) == sizeof(int)
, a char is promoted to an unsigned int;
some_char + 1
does not result in undefined behaviour.
On the contrary, if the default char is signed, and
sizeof(char) == sizeof(int)
, a char is promoted to an int;
some_char + 1
can result in undefined behaviour.
> The implementation of how numerical values are represented in binary is not dictated by C++.
The C++ standard imposes strict requirements on how integral types are to be represented.
The representations of integral types shall deﬁne values by use of a pure binary numeration system. (Footnote: A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position.)
[ Example: this International Standard permits 2’s complement, 1’s complement and signed magnitude representations for integral types. — end example ] - IS |
> (1<<2) takes the binary representation of the literal 1 and shifts it left 2 places.
> That will only == 4 if the resulting binary number matches the binary representation of the literal 4.
> It is true for 2s compliment and 1s compliment integers...
>but if the target machine is using some kind of insane representation it may not be true.
This is always true (in C++).
The value of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are zero-ﬁlled. If E1 has an unsigned type, the value of the result is E1 × 2^{E2}, reduced modulo one more than the maximum value representable in the result type. Otherwise, if E1 has a signed type and non-negative value, and E1 × 2^{E2} is representable in the result type, then that is the resulting value; otherwise, the behavior is undeﬁned. - IS |
In this specific case,
(1<<2),
1*2^{2} is representable in the result type, and that is guaranteed to be the resulting value.