May 15, 2017 Re: avoid extra variable during void pointer cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marco Leise | 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@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.
|
May 17, 2017 Re: avoid extra variable during void pointer cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bauss | Am Mon, 15 May 2017 19:30:00 +0000 schrieb Bauss <jj_1337@live.dk>: > 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. A compiler doesn't simply inline whenever it can. A big function that's called often would lead to massive code duplication in that case. What I meant pragma(inline, true) to do is overrule this cost calculation. Since the OP asked for no extra function calls, the error on failure to inline seemed appropriate. Cross-module inlining may fail for example on some compiler(s) or with separate compilation. -- Marco |
Copyright © 1999-2021 by the D Language Foundation