Re: Inappropriate casting?

From: Rodney B. Elliott (
Date: 06/10/04

Date: 10 Jun 2004 13:30:37 -0700

Interesting - I don't know if I agree with your summary of the
fundamental types though Francis. What you are implying is that *all*
C++ programmers of any experience allocate fundamental types based
upon the criteria you suggest - and is this really the case? As you
will be aware, my C++ experience is small so I am tempted to take your
word for it, but I cannot help but be influenced by the programming I
have done - namely assembly language. In this environment I try very
hard to make my code as small and as fast as I possibly can, and thus
all my variables are only as big as they need to be, and there are
lots of unsigned 8-bit values spread amongst them. Thus when faced
with trying to do what Stroustrupp suggests and make my *code* the
comment, it seemed natural to use an unsigned char as:

a. It clearly shows that the value fed to the function must be 8-bit.
b. Negative numbers are verboten.

As far as I can see, the only real disadvantage from this approach is
that if the 3rd party library is upgraded later on to actually use
16-bit values (unsigned shorts) I will have to change my code.

> Unfortunately it isn't because other common idioms hide this intent.

Care to elaborate a little bit?
> Basically anytime you find your coding style results in lots of casts
> ask yourself if what you are doing is worth having to tell the compiler
> so often that you are the boss:-)

My intent is not to beat a message into the compiler, but rather to
indicate clearly to the user/reader of the source code the range of
acceptable values.