Re: Inappropriate casting?

From: Rodney B. Elliott (rodney.elliott_at_canterbury.ac.nz)
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.



Relevant Pages

  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... what experienced programmers do, ... optimization, ... Thugs" ad nauseum fits that a lot more closely than discussing compiler ... be modified outside a loop, and guessing ...
    (comp.programming)
  • Re: Statement on Schildt submitted to wikipedia today
    ... to working programmers and more with being "right all the time, ... so that compiler developers could be shed ... The major corporate interests were compiler developers, ... processors, committed to standard division semantics, and otherwise ...
    (comp.lang.c.moderated)
  • Re: Interesting article by Randall Hyde
    ... as you are the compiler optimization "expert". ... of the work that has gone into creating optimizing compilers. ... > Compilers generate better code than *most* programmers all of the ... learn without needing to know anything about assembly language. ...
    (comp.lang.asm.x86)
  • Re: FC4 good new tech, bad legacy support
    ... Often programmers abuse weaknesses in the strictness of how a ... >compiler implements a programming language. ... because an older compiler version seems to accept source code ... >which it should not accept according to programming language standards. ...
    (Fedora)
  • Re: Integer types in embedded systems
    ... Notice the integer type I've used, ... compilers for 8-bit micros have an option for telling the compiler not ... Most C programmers these days are woefully ignorant, ... At least as important as portability, ...
    (comp.lang.c)