Static cast
Jason House
jason.james.house at gmail.com
Wed Jun 25 17:33:49 PDT 2008
Frank Benoit Wrote:
> 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.
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.
More information about the Digitalmars-d-learn
mailing list