[gclist] Finalizer flame wars.
Amit J Patel
amitp@Theory.Stanford.EDU
Mon, 13 May 1996 16:00:56 -0700
Robert A. Duff wrote to me on May 10:
> >... In C++
> > type casts occur almost every line.
>
> Good example. In Ada, you can do the same sort of dangerous casts as in
> C++. However, the "dangerous" casts use a different syntax from the
> "safe" ones, so a person reading the code can tell what's going on.
> IMHO, this is a better solution than simply forbidding evil.
>
> - Bob
Perhaps I just hang around the wrong programmers, but I never see these
often-referenced programs that cast "almost every line". I have just
about as many casts in my C++ programs as I have in my Pascal or ML
programs: zero except in libraries that interface to the OS.
C++ also uses different syntaxes for different types of casts from A
to B:
static_cast<B>(A a) ==> safe; implicit (like int->float)
const_cast<B>(A a) ==> change the const-ness
reinterpret_cast<B>(A a) ==> the dangerous one
dynamic_cast<B>(A a) ==> safe; not implicit
I think that reinterpret_cast is the only one that causes problems for
a non-conservative garbage collector (like turning pointers into
ints), but I'm not sure about that. (Interior pointers may cause
trouble too -- for example, in pointer arithmetic.)
- Amit
--
Amit J Patel, Computer Science Department, Stanford University
"The works of a lone architect are more elegant
than those attempted by several together." - Pascal