rtti cast

Janice Caron caron800 at googlemail.com
Thu May 8 06:51:09 PDT 2008


2008/5/8 terranium <spam at here.lot>:
>  cast is not a typecheck, it's a type conversion.

In C++, there is a difference between a regular cast and a
dynamic_cast. The expression dynamic_cast<T>(x) performs a run-time
type check and type conversion. See
http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/topic/com.ibm.vacpp6m.doc/language/ref/clrc05keyword_dynamic_cast.htm

dynamic_cast<T>() returns a null pointer of type T if the cast fails.
Users of dynamic_cast<T>() are therefore required always to check the
return value for null. This is not an indication of a bug - it is
designed behavior.

Thus, in C++

(1) traditional cast is not a typecheck, it's a type conversion
(2) dynamic cast is a a runtime type check

Unfortunately, D's cast(T)x behaves exactly like C++'s
dynamic_cast<T>(x) when it comes to casting up and down a class
heirarchy. D only has one kind of cast, and it does both jobs. So
Jarrett is using D's casts correctly, /but/ D's casts are themselves
flawed. The "one cast that does everything" ideology is just a tool
for making bugs.



More information about the Digitalmars-d mailing list