avoid extra variable during void pointer cast

Bauss via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 15 12:30:00 PDT 2017


On Sunday, 14 May 2017 at 21:07:36 UTC, Marco Leise wrote:
> Am Sun, 14 May 2017 20:18:24 +0000
> schrieb Kevin Brogan <kevin at brogan.ca>:
>
>> [...]
>
> No, that is not possible. An alias can only be assigned a 
> symbol.
>
>> [...]
>
> Let the compiler optimize the assignment away and don't worry 
> much about it. Inlining also works well within the same module. 
> In this case here I would probably use "consume" functions as I 
> dub them:
>
> 	import std.traits;
> 	pragma(inline, true) /* Not really needed I hope ;) */
> 	ref T consume(T)(ref void* data) if (!hasIndirections!T)
> 	{
> 		T* ptr = cast(T*)data;
> 		data += T.sizeof;
> 		return *ptr;
> 	}
>
> You can then rewrite your addInt function like this:
>
> 	void add(T)(void* state, void* data) if (isNumeric!T)
> 	{
> 		state.consume!T += data.consume!T;
> 	}

pragma(inline, true); doesn't actually do what you think it does. 
In lining is always done whenever possible and that only tells 
the compiler to spit out an error if it can't inline it.



More information about the Digitalmars-d-learn mailing list