From: Darin Adler (darin_at_[hidden])
Date: 2001-09-24 11:13:04
on 9/24/01 7:02 AM, Fernando Cacciola at fcacciola_at_[hidden] wrote:
> 'int' is signed, but the 'number of bits' is a finite quantity.
> Why use a *signed* integer to represent a finite quantity? Just becuase the
> maxiumn size fits the maximun positive number representable with the signed
> type? I don't think so. I think that values that cannot be negative should
> be represented with unsigned types.
> Consider, for instance, the std::bitset interface; or consider
> 'integer_mask.hpp', also in this review.
I understand that you prefer to use an unsigned type for numbers that can't
I don't think that size_t is specifically called for, because it is both too
small (it can't necessarily hold the total number of bits in the largest
possible object, since that's 8 times larger than the number of bytes), and
too large (a simple unsigned int is likely to be more efficient on platforms
where size_t is very large and has plenty of range for this purpose).
I personally don't use unsigned types for numbers that can't be negative for
pragmatic reasons. Putting the edge of the space of representable numbers
right next to the range of values that are used makes it easy to make errors
like this common one:
for (unsigned i = size; i > 0; i--)
which is an infinite loop if i is an unsigned variable. If you try to
rewrite that for statement to work properly for an unsigned i, you'll see
why some people prefer not to use unsigned types even when the value is
always nonnegative. I typically use signed types to avoid this and other
related issues (and I also don't write loops that look like that).
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk