digitalmars.D - shouldn't cast(Object)cast(void*)anything work?
- Davidl (52/52) Apr 04 2007 for something with the same size as a pointer like , int, int*, i hope i...
- Johan Granberg (14/72) Apr 04 2007 This works for me using gdc 0.23
- Davidl (2/2) Apr 04 2007 i use dmd ;)
- Brad Roberts (9/45) Apr 04 2007 There's no extra 'runtime' anything involved here, just a more concrete
- Davidl (23/23) Apr 04 2007 i don't think compiler could eliminate the following :
for something with the same size as a pointer like , int, int*, i hope i= t = 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 a= t = all without union trick(even u can't cast void* to it) . This is really uncomfortabl= e. = coz extra runtime var would be required consider : class Expression{} Expression EXP_CANT_INTERPRET =3D 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=3D1; } 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=3D1; } _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=3D1; //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?
Apr 04 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?
Apr 04 2007
i use dmd ;) it's astonishing for me to know gdc compiles it
Apr 04 2007
On Thu, 5 Apr 2007, Davidl wrote: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 varThere's no extra 'runtime' anything involved here, just a more concrete expression of what you're trying to express than casting.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_INTERPRETThe 'problem' here is the initializer you've specified on the second member of the union. Take that out and the message would go away I suspect. I also suspect, though haven't tried, that if the int i was first rather than second, that the error would also go away. Later, Brad
Apr 04 2007
i don't think compiler could eliminate the following : union __exp { Expression _m_exp; int i; } __exp EXP_CANT_INTERPRET static this() { EXP_CANT_INTERPRET.i=3D1; } to : simply cast(Expression)1; while in runtime return cast(Expression)1; would generate literal 1 and retn and the example use union trick could possibly need to copy a var which = = holds the value of 1 and it's strange for the error message goes away when it becomes the fir= st = member ;) :o because the first member's initializer is the union's default = initializer?
Apr 04 2007