avoid extra variable during void pointer cast

Moritz Maxeiner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 14 14:35:00 PDT 2017


On Sunday, 14 May 2017 at 21:16:04 UTC, Stanislav Blinov wrote:
> 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;
> }

That's a pretty cool workaround, but not an alias to the cast, 
but an alias to a parametrized function template (a type), so I 
will stick to my statement of "not possible". AFAIK, this would 
also invoke the respective function template instance for ptrCast 
every time _state or _data are referenced, so if we're going with 
the spirit of the question that's still going to be horrible if 
the compiler doesn't optimize ;p



More information about the Digitalmars-d-learn mailing list