shouldn't cast(Object)cast(void*)anything work?

Johan Granberg lijat.meREM at OVEgmail.com
Wed Apr 4 14:27:04 PDT 2007


Davidl wrote:

> for something with the same size as a pointer like , int, int*, i hope it
> can be
> directly cast to cast(void*), coz nowadays d has a particularly strict
> cast system
> and the casting something not Object to any Class now seems impossible at
> all without
> union trick(even u can't cast void* to it) . This is really uncomfortable.
> coz extra
> runtime var would be required
> 
> consider :
> class Expression{}
> Expression EXP_CANT_INTERPRET = cast(Expression)cast(void*)1; // this
> won't compile
> 
> you need to:
> 
> union __exp
> {
>        Expression _m_exp;
>        int i;
> }
> 
> __exp EXP_CANT_INTERPRET;
> 
> static this()
> {
>      EXP_CANT_INTERPRET.i=1;
> }
> 
> and i don't get why i can't use the following work:
> 
> //in order to get rid of the runtime extra var
> 
> union _EXP_CANT_INTERPRET  // coz we can't use an union as an instance
> {
>      Expresssion _m_exp;
>      int i=1;
> }
> _EXP_CANT_INTERPRET EXP_CANT_INTERPRET;  //compiler complains overlap
> initializer?? strange
> 
> and this won't work either:
> 
> struct _EXP_CANT_INTERPRET
> {
>     union
>     {
>         Expression _m_exp;
>         int i=1;              //this would either complain overlap initializer of
> struct _EXP_CANT_INTERPRET
>     }
> }
> _EXP_CANT_INTERPRET EXP_CANT_INTERPRET
> 
> any effort of bringing EXP_CANT_INTERPRET to compile time would be
> impossible,
> that's quite painful IMO. Did i miss some better solutions?

This works for me using gdc 0.23

void main()
{
        Object t=new Object;
        int* i=cast(int*)t;
        int d;
        t=cast(Object)d;
        t=cast(Object)i;
        printf("%*s\n",t.toString());
}

prints

object.Object

So what are you trying to do and which compiler and libraries are you using?



More information about the Digitalmars-d mailing list