[comp.lang.misc] Automatic type conversion

rfm@urth (Rich McAllister) (04/10/91)

In article <327@heurikon.heurikon.com>, daves@ex (Dave Scidmore) writes:
[in reply to David Cok's statement that a good programming language
allows most programs to be written without casts]
>As a rule, I tend to like people who write programs I might be required to
>maintain to make as explicit as possible any subtle changes of type which
>I might not immediately grasp on first examination of the code. Casts are
>a means of making clear to other programmers what the original programmer
>intended. In the absence of casts when hunting down a bug I must constantly
>ask myself if the original programmer intended the "hidden" type conversion
>to occur, or whether the programmer was unaware that the type conversion
>could be taking place. When a cast is provided I have a much higher degree
>of confidence that what I see happening in the code was what was actually
>intended.

Automatic type conversions are a source of many subtle errors.  PL/I,
as in so many other things, is a classic bad example.  PL/I would
generally try to convert anything to anything, so that the following
program fragment would actually compile and execute:

    FIXED BINARY A, B;
    CHARACTER C(3);

    A = "001";
    B = "002";
    C = A+B;

Of course the value in C probably wouldn't be "003" because of the bizarre
default precision rules which I've mostly forgotten, but the compiler really
would generate decmial-to-binary conversions.  Experienced programmers
learned the names of the library routines PL/I used for automatic
conversions, and always looked for them in the link map if things were
broken or running real slow.  Eventually the compiler was changed to warn
about them.  So my experience is automatic conversions can really hurt, and
it's the one C++ feature I use but feel scared about using...

This discussion has drifted away from strict C++ issues, so I've
directed followups to comp.lang.misc.

Rich McAllister (rfm@eng.sun.com)