|
Boost : |
From: Topher Cooper (topher_at_[hidden])
Date: 2006-06-02 18:40:00
I don't think that it is that important to have unsigned_integer, but
this argument against it doesn't stand up:
1) Unsigned integer is not just an arbitrary restriction on range but
an important concept in its own right. Its use clarifies intent. In
my opinion, for example, it is a common, though minor, programming
error to use "int" rather than "unsigned int" when you intend to
express cardinality of something (e.g., the size of an array). The
only real justification otherwise is to allow such practical "tricks"
as testing if a decreasing index is less than 0.
2) The extra data type, assuming that the design error of deriving
unsigned_integer from integer is avoided, would add complexity for
boost maintainers but virtually none for users. It is, after all a
logical extension of the built in types -- it takes the user all of 5
seconds to understand it, if that. Good library design is
user-centric. Developer-centric design is the root of a great deal
of evil. Lack of development/maintenance resources is, of course, a
valid argument, but this is a completely different issue than that of
the evils of unnecessary complexity.
3) Normal use of an unsigned integer type will never "try" to produce
a negative result -- its not a meaningful concept, so if you play by
the rules it doesn't happen. You certainly would never try to negate
a cardinality (how often do you write code that says "If this string
contains 27 characters then I want another one containing -27
characters?). Subtraction does have to be done with care, but in
most cases the issue of subtracting a larger from a smaller doesn't
come up. Subtraction generally appears, sometimes subtly so, as a
computation of the size of the complement of a subset within a larger
set. If you are dealing with where you would want to use unsigned
integers and you unwittingly do subtract a larger from a smaller
value -- if your program logic does not protect you from it -- than
you better have an exception.
4) If the exceptions are exceptional than in most systems it is very
much more expensive to do an explicit test than to set up to handle
an exception.
Topher Cooper
At 04:09 PM 6/2/2006, you wrote:
>Unsigned integers shouldn't exist. If the programmer wants to check
>whether the number is greater than 0, or greater than 42, he can do it
>himself a lot faster than a try{} catch{} block in terms of both
>programming and runtime. Creating the extra datatype would add
>completely unnecessary complexity to the situation. Unnecessary
>complexity is the root of all evil.
>_______________________________________________
>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