CTFE and cast

k2 k2 at not.com
Tue Jan 17 01:13:43 PST 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 HANDLE
> 
> It'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.
> 
> 
> 



More information about the Digitalmars-d-learn mailing list