|
Boost : |
From: Wynand Winterbach (wynand_at_[hidden])
Date: 2003-10-30 08:40:30
On Wed, Oct 29, 2003 at 10:13:14PM -0700, Dave Gomboc wrote:
> > so where should I use unsigned?
> >
> > -Thorsten
>
> Use unsigned numeric types whenever negative values aren't
> permitted or don't make sense.
>
> Dave
>
I quote Bjarne Stroustrup (page 73, The C++ Programming Language, Third edition)
"The unsigned integer types are ideal for uses that treat storage as a bit array.
Using an unsigned instead of an int to gain one more bit to represent positive
integers is almost never a good idea. Attempts to ensure that some values are
positive by declaring variables unsigned will typically be defeated by the
implicit conversion rules"
As an example that got me when I started programming, consider the following
seemingly innocuous case:
char blah[6];
for ( unsigned i = 5; i >= 0; i-- )
{
cout << blah[i];
}
This fails i can never become -1. Actually, you'll have an infinite loop.
--Wynand
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk