Boost logo

Boost :

From: Thorsten Ottosen (nesotto_at_[hidden])
Date: 2003-10-30 09:08:05


"Wynand Winterbach" <wynand_at_[hidden]> wrote in message
news:20031030154030.B9878_at_dip.sun.ac.za...
> 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)

Actually, one of the to threads I mentioned was called "unsigned vs signed -
Is Bjarne Mistaken?"

I believe the answer is "no".

> "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.

there was a lot of similar examples in the threads.

-Thorsten


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