digitalmars.D - Re: rtti cast
- terranium <spam here.lot> May 08 2008
- "Janice Caron" <caron800 googlemail.com> May 08 2008
- terranium <spam here.lot> May 08 2008
Jarrett Billingsley Wrote:I've only ever used downcasts in D as a replacement for Java's "instanceof", where it's just a simple typecheck.
cast is not a typecheck, it's a type conversion.
May 08 2008
2008/5/8 terranium <spam 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.
May 08 2008
Janice Caron Wrote:(1) traditional cast is not a typecheck, it's a type conversion (2) dynamic cast is a a runtime type check
May 08 2008