Getting started

Alex Martelli aleax at aleax.it
Thu Sep 26 14:35:26 CEST 2002


James J. Besemer wrote:
        ...
> Types are otherwise pretty secure in C++ except the "cast" mechanism
> "foils" the type system and thus allows type errors to occur.

Actually, unions are at least as bad -- they're not often used
for type-punning because casts are handier for that, but if you
took cast aways, people would (ab-)use unions (just like they
abuse EQUIVALENCE in Fortran for similar purposes).

Technically I believe that in C++ (not sure about the two ISO
C standards -- I'm more definite that this was the case in
Fortran-77 and I believe in Fortran-IV too) storing one union's
member then retrieving another one puts your program in
undefined-behavior land.  In practice, I know of no existing
compiler that goes to the bother of checking (e.g. by storing
a hidden tag when you store into a union and checking whenever
you retrieve from it -- even harder if you consider _pointers_
to the various members of the union).


Alex




More information about the Python-list mailing list