digitalmars.D.learn - CTFE and cast
- k2 (19/19) Jan 13 2012 When replace typedef to enum, it became impossible to compile a certain
- Don Clugston (5/28) Jan 13 2012 It's a problem. Casting integers to pointers is a very unsafe operation,
- Piotr Szturmaj (16/45) Jan 13 2012 Do you plan to support endianness handling at CTFE?
- k2 (3/35) Jan 17 2012 Where can I find a "special hack"?
When replace typedef to enum, it became impossible to compile a certain portion. dmd v2.057 Windows -------------------------------------------- enum HANDLE : void* {init = (void*).init} pure HANDLE int_to_HANDLE(int x) { return cast(HANDLE)x; } void bar() { HANDLE a = cast(HANDLE)1;// ok HANDLE b = int_to_HANDLE(2);// ok } HANDLE c = cast(HANDLE)3;// ok HANDLE d = int_to_HANDLE(4);// NG -------------------------------------------- foo.d(17): Error: cannot implicitly convert expression (cast(void*)4u) of type void* to HANDLE
Jan 13 2012
On 13/01/12 10:01, k2 wrote:When replace typedef to enum, it became impossible to compile a certain portion. dmd v2.057 Windows -------------------------------------------- enum HANDLE : void* {init = (void*).init} pure HANDLE int_to_HANDLE(int x) { return cast(HANDLE)x; } void bar() { HANDLE a = cast(HANDLE)1;// ok HANDLE b = int_to_HANDLE(2);// ok } HANDLE c = cast(HANDLE)3;// ok HANDLE d = int_to_HANDLE(4);// NG -------------------------------------------- foo.d(17): Error: cannot implicitly convert expression (cast(void*)4u) of type void* to HANDLEIt's a problem. Casting integers to pointers is a very unsafe operation, and is disallowed in CTFE. There is a special hack, specifically for Windows HANDLES, which allows you to cast integers to pointers at compile time, but only after they've left CTFE.
Jan 13 2012
Don Clugston wrote:On 13/01/12 10:01, k2 wrote:Do you plan to support endianness handling at CTFE? I mean to write something like this at CT: union U { ubyte[4] ar; uint num; } U u; u.num = 0x04030201; if (u.ar[0] == 1) // little endian else // big endian This is needed to support crypto hashing at CT. std.uuid can generate uuids based on strings, but they must be hashed first.When replace typedef to enum, it became impossible to compile a certain portion. dmd v2.057 Windows -------------------------------------------- enum HANDLE : void* {init = (void*).init} pure HANDLE int_to_HANDLE(int x) { return cast(HANDLE)x; } void bar() { HANDLE a = cast(HANDLE)1;// ok HANDLE b = int_to_HANDLE(2);// ok } HANDLE c = cast(HANDLE)3;// ok HANDLE d = int_to_HANDLE(4);// NG -------------------------------------------- foo.d(17): Error: cannot implicitly convert expression (cast(void*)4u) of type void* to HANDLEIt's a problem. Casting integers to pointers is a very unsafe operation, and is disallowed in CTFE. There is a special hack, specifically for Windows HANDLES, which allows you to cast integers to pointers at compile time, but only after they've left CTFE.
Jan 13 2012
Where can I find a "special hack"? thanks. (2012/01/13 21:58), Don Clugston wrote:On 13/01/12 10:01, k2 wrote:When replace typedef to enum, it became impossible to compile a certain portion. dmd v2.057 Windows -------------------------------------------- enum HANDLE : void* {init = (void*).init} pure HANDLE int_to_HANDLE(int x) { return cast(HANDLE)x; } void bar() { HANDLE a = cast(HANDLE)1;// ok HANDLE b = int_to_HANDLE(2);// ok } HANDLE c = cast(HANDLE)3;// ok HANDLE d = int_to_HANDLE(4);// NG -------------------------------------------- foo.d(17): Error: cannot implicitly convert expression (cast(void*)4u) of type void* to HANDLEIt's a problem. Casting integers to pointers is a very unsafe operation, and is disallowed in CTFE. There is a special hack, specifically for Windows HANDLES, which allows you to cast integers to pointers at compile time, but only after they've left CTFE.
Jan 17 2012