Zero-cost version-dependent function call at -O0.

Anonymouse via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 25 09:29:20 PDT 2017


On Sunday, 25 June 2017 at 15:58:48 UTC, Johan Engelen wrote:
> How would you solve this problem: do an optional function call 
> depending on some version(X). If version(X) is not defined, 
> there should be no call and no extra code at -O0.
>
> ```
> {
> ...
>    foo();  // either compiles to a function call, or to 
> _nothing_.
> ...
> }
> ```
>
> In C, you could do something like:
> ```
> #if X
>   void foo() {......}
> #else
>   #define foo()
> #endif
> ```
>
> How would you do this in D?
>
> I can think of `mixin(foo())` but there is probably a nicer way 
> that preserves normal function calling syntax.
>
> Cheers,
>   Johan

Am I missing something, or can't you just version both the 
function and the function ćall?

version(X)
void foo() { /* ... */ }

void main()
{
     version(X)
     {
         foo();
     }
}


More information about the Digitalmars-d-learn mailing list