digitalmars.D.learn - Static cast
- Jason House (2/2) Jun 25 2008 What is the proper way to implement static cast?
- BCS (5/11) Jun 25 2008 up cast or down cast?
- Jason House (5/16) Jun 25 2008 Both. Upcasting to an interface shows up in my profiler.
- BCS (11/35) Jun 25 2008 I'm not sure there /is/ a way to do non dynamic casts to/from interfaces...
- Jarrett Billingsley (8/12) Jun 25 2008 Doesn't honor vtable locations? Are you talking about trying to cast an...
- Jason House (2/16) Jun 25 2008 Yes, interfaces too. You're right that casts to/from an interface is the...
- Frank Benoit (17/20) Jun 25 2008 I you need this only for special classes and not as a general thing...
- Jason House (2/27) Jun 25 2008 I'd template the algorithms by the data type before doing that type of a...
What is the proper way to implement static cast? cast(T) to shift with an object/ interface hierarchy is dynamic and slow. My problem is that cast(T)cast(void*) does not honor vtable locations and ClassInfo is only available at runtime.
Jun 25 2008
Reply to Jason,What is the proper way to implement static cast? cast(T) to shift with an object/ interface hierarchy is dynamic and slow. My problem is that cast(T)cast(void*) does not honor vtable locations and ClassInfo is only available at runtime.up cast or down cast? IIRC upcast /is/ non-dynamic and skipping the test on downcast is just a bad idea. Or am I not reading you correctly?
Jun 25 2008
&BCS Wrote:Reply to Jason,;What is the proper way to implement static cast? cast(T) to shift with an object/ interface hierarchy is dynamic and slow. My problem is that cast(T)cast(void*) does not honor vtable locations and ClassInfo is only available at runtime.up cast or down cast?Both. Upcasting to an interface shows up in my profiler.IIRC upcast /is/ non-dynamic and skipping the test on downcast is just a bad idea.I've implemented algorithms to operate on interfaces. Operations called by the algorithm require casting. All the data operated on by an algorithm is the same type. In this case, it's safe and will speed up execution by at least 15%
Jun 25 2008
Reply to Jason,&BCS Wrote:I'm not sure there /is/ a way to do non dynamic casts to/from interfaces. For one thing an interface reference does not reference the same address as the object. interface I {} class C : I {} C c = new C; assert(cast(void*) c != (cast(void*)cast(I) c); // not actually tested If you have a slew of casts from/to the _exact_same_types_ to do, you might cast the first one, find the pointer difference, and then just add that for the rest of the casts. But if any of your assumptions are off, your toast.Reply to Jason,;What is the proper way to implement static cast? cast(T) to shift with an object/ interface hierarchy is dynamic and slow. My problem is that cast(T)cast(void*) does not honor vtable locations and ClassInfo is only available at runtime.up cast or down cast?Both. Upcasting to an interface shows up in my profiler.IIRC upcast /is/ non-dynamic and skipping the test on downcast is just a bad idea.I've implemented algorithms to operate on interfaces. Operations called by the algorithm require casting. All the data operated on by an algorithm is the same type. In this case, it's safe and will speed up execution by at least 15%
Jun 25 2008
"Jason House" <jason.james.house gmail.com> wrote in message news:g3uedt$1vn0$1 digitalmars.com...What is the proper way to implement static cast? cast(T) to shift with an object/ interface hierarchy is dynamic and slow. My problem is that cast(T)cast(void*) does not honor vtable locations and ClassInfo is only available at runtime.Doesn't honor vtable locations? Are you talking about trying to cast an interface to a class reference, or an interface to a derived interface? Since that's the only scenario I can think of where the resulting reference would have invalid vtbl locations. If you're casting from a base *class* to a derived *class*, there is no way AFAIK that it could end up with invalid vtbl entries.
Jun 25 2008
Jarrett Billingsley Wrote:"Jason House" <jason.james.house gmail.com> wrote in message news:g3uedt$1vn0$1 digitalmars.com...Yes, interfaces too. You're right that casts to/from an interface is the only problematic scenario.What is the proper way to implement static cast? cast(T) to shift with an object/ interface hierarchy is dynamic and slow. My problem is that cast(T)cast(void*) does not honor vtable locations and ClassInfo is only available at runtime.Doesn't honor vtable locations? Are you talking about trying to cast an interface to a class reference, or an interface to a derived interface? Since that's the only scenario I can think of where the resulting reference would have invalid vtbl locations. If you're casting from a base *class* to a derived *class*, there is no way AFAIK that it could end up with invalid vtbl entries.
Jun 25 2008
Jason House schrieb:What is the proper way to implement static cast? cast(T) to shift with an object/ interface hierarchy is dynamic and slow. My problem is that cast(T)cast(void*) does not honor vtable locations and ClassInfo is only available at runtime.I you need this only for special classes and not as a general thing... you can probably give those classes/interfaces a asType Method. interface I1 { T asT(); } interface I2 : I1 { } class A : I2 { T asT(){ return null; } } class B : A{} class T : A{ T asT(){ return this; } } Now the cast is only a virtual method call and no dynamic cast is needed and you get null for class which are not T.
Jun 25 2008
Frank Benoit Wrote:Jason House schrieb:I'd template the algorithms by the data type before doing that type of a solution. Templates are the fastest option anyway. It should also allow function inlining or removal of virtual function calls which may be a significant speed boost.What is the proper way to implement static cast? cast(T) to shift with an object/ interface hierarchy is dynamic and slow. My problem is that cast(T)cast(void*) does not honor vtable locations and ClassInfo is only available at runtime.I you need this only for special classes and not as a general thing... you can probably give those classes/interfaces a asType Method. interface I1 { T asT(); } interface I2 : I1 { } class A : I2 { T asT(){ return null; } } class B : A{} class T : A{ T asT(){ return this; } } Now the cast is only a virtual method call and no dynamic cast is needed and you get null for class which are not T.
Jun 25 2008