Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2001-09-24 18:57:24


Before you guys get too involved in this debate, please realize that the
part of dlw_int.zip you're arguing about was _not_ written by me. It is an
existing part of the integer library, and I appended new stuff onto that
file. The design decisions for those class templates have already been
made.

(I guess that the number-of-bits parameter is an 'int' because 'int' is the
type that 'std::numeric_limits<>' gives to bit-sizes.)

on 9/24/01 9:13 AM, Darin Adler at darin_at_[hidden] wrote:

> 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
> be negative.
>
> 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).

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk