c++ - Using -1 as a flag value for unsigned (size_t) types -
i using -1 flag value function return type size_t (an unsigned type).
i didn't notice @ first, particularly because wasn't causing errors in code (i checking x == -1, not x < 0).
are there subtle reasons shouldn't leave is? when might behave unexpectedly? commonly used?
ptrdiff_t less common, takes longer type, , anyway it's not appropriate type since function returns index array.
-1
convert max unsigned value, due section 4.7
integral conversions:
if destination type unsigned, resulting value least unsigned integer congruent source integer (modulo 2n n number of bits used represent unsigned type). [ note: in two’s complement representation, conversion conceptual , there no change in bit pattern (if there no truncation). —end note ]
the same quote c99 6.3.1.3
:
otherwise, if new type unsigned, value converted repeatedly adding or subtracting 1 more maximum value can represented in new type until value in range of new type.49)
so end with:
-1 + (umax + 1)
which is:
umax
Comments
Post a Comment