avoid extra variable during void pointer cast

Stanislav Blinov via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 14 14:16:04 PDT 2017


On the point of "not possible...", "only a symbol...", etc:

T* ptrCast(T, alias ptr)() { return cast(T*)ptr; }

void addInt(void* state, void* data)
{
     alias _state = ptrCast!(int, state);
     alias _data = ptrCast!(int, data);

     static assert(!is(typeof(_state) == int*));
     static assert(!is(typeof(_data) == int*));

     *_state += *_data;
}

But take heed to the compiler optimization advice. DMD generates 
pretty horrendous code for this. LDC does rather well though. 
Since speed matters, always look at the assembly. Look at it even 
if it doesn't ;)


More information about the Digitalmars-d-learn mailing list