CTFE and Static If Question
jmh530
john.michael.hall at gmail.com
Thu May 7 16:58:22 UTC 2020
On Thursday, 7 May 2020 at 15:34:21 UTC, ag0aep6g wrote:
> [snip]
>
> The `static if` is guaranteed to be evaluated during
> compilation. That means, `foo!y0` effectively becomes this:
>
> auto foo(int rt) { return rt + 1; }
>
> There is no such guarantee for `foo(rt, y0)`. It doesn't matter
> that y0 is an enum.
>
> But a half-decent optimizer will have no problem replacing all
> your calls with their results. Compared with LDC and GDC, DMD
> has a poor optimizer, but even DMD turns this:
>
> int main() {
> int rt = 3;
> bool x0 = true;
> bool x1 = false;
> enum y0 = true;
> enum y1 = false;
> return
> foo(rt, x0) +
> foo(rt, x1) +
> foo!y0(rt) +
> foo!y1(rt) +
> foo(rt, y0) +
> foo(rt, y1);
> }
>
> into this:
>
> int main() { return 21; }
Thanks for the reply.
The particular use case I'm thinking of is more like below where
some function bar (that returns a T) is called before the return.
Not sure if that matters or not for inlining the results.
T foo(T[] x, bool y) {
if (y)
return bar(x) / x.length;
else
return bar(x) / (x.length - 1);
}
More information about the Digitalmars-d-learn
mailing list