From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2001-09-24 14:47:12
----- Original Message -----
From: Darin Adler <darin_at_[hidden]>
To: Boost <boost_at_[hidden]>
Sent: Monday, September 24, 2001 1:13 PM
Subject: [boost] Number of bits: int vs. size_t
> 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
> > maxiumn size fits the maximun positive number representable with the
> > type? I don't think so. I think that values that cannot be negative
> > 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
> be negative.
> I don't think that size_t is specifically called for, because it is both
> 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),
But this problem is not specific of size_t but to any fixed type.
> too large (a simple unsigned int is likely to be more efficient on
> where size_t is very large and has plenty of range for this purpose).
Remember that we are talking about a template parameter, not a runtime
value, so efficiency in this context means the effort and time required to
compile it. I don't think that using size_t instead of int as a template
parameter can actually slow down compilation.
Anyway, your point would be to use 'unsigned int' instead of 'size_t', and I
would agree with it, but IMO 'int' shouldn't be used.
> I personally don't use unsigned types for numbers that can't be negative
> 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
> 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).
I am aware of those issues, and I agree that an unsigned type might be the
wrong choice in certain situations.
However, I think that the right solution is not to entirely forget about
unsigned types but to use them where really appropiate.
In the particular case of the templates in question, I don't see any
potential danger in using an unsigned type so I think it should be used.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk