Boost logo

Boost :

From: Maarten Kronenburg (M.Kronenburg_at_[hidden])
Date: 2006-06-04 07:11:50


Carlo,
Yes the unsigned_integer decrement does
a test in the predecrement operator,
but it is only the test of the sign,
which will be fast.
Users that are VERY performance critical
then still have the option of not using
unsigned_integer.
Yes the unsigned_integer is not a proper
group; on the other hand, when a user
runs the following program:
unsigned int x = -2;
cout << x << endl;
and the user gets
4294967294
then most users will be surprised,
and not uderstand what is going on.
The following program:
unsigned_integer x = -2;
cout << x << endl;
will generate an error, which in my
opinion is still more intuitive for most users
who don't know what a modular integer is.
Regards, Maarten.

"Carlo Wood" <carlo_at_[hidden]> wrote in message
news:20060604020720.GA25970_at_alinoe.com...
> On Sat, Jun 03, 2006 at 02:54:53PM +0200, Maarten Kronenburg wrote:
> > The assertion for unsigned_integer
> > is an interesting possibility.
> > In "C++ in a nutshell" I read:
> > "If the expression evaluates to 0,
> > assert prints a message to the standard
> > error file and calls abort."
> > Personally I would rather throw an
> > exception, so that the user can determine
> > what should happen if an unsigned_integer
> > becomes negative.
>
> There is nothing to decide, it is a bug when that happens.
> The best you can do is to dump a core (which is what assert does)
> so that the developer can examine as close as possible to
> the bug what has happened.
>
> Once the program works, an integer that should never
> become negative will never become negative anymore,
> and no testing for less than zero is needed.
> You can still use a normal signed integer and just omit
> the test (assertion).
>
> Thus,
>
> void Foo::release(void)
> {
> assert(x > 0);
> if (--x == 0)
> do_something();
> }
>
> where x can be perfectly a signed integer, and no
> testing overhead is done in the final application.
>
> rather than
>
> void Foo::release(void)
> {
> if (--x == 0)
> do_something();
> }
>
> where x is a special type, solely to throw an exception
> for the case that the program is bugged, needing not
> only a total new type (with no mathematical meaning,
> and no simularity with builtin-types*) ), but also the
> cost of testing as part of the pre-decrement operator,
> which can't be removed from the final application.
>
> *) Because, at least unsigned int is modular:
> both int (assuming no overflow occurs) and unsigned
> int are rings. While an infinite precision unsigned
> integer wouldn't even be a proper group.
>
> --
> Carlo Wood <carlo_at_[hidden]>
> _______________________________________________
> Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost
>


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