[Issue 18558] Template alias spec incomplete

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Mar 5 16:17:58 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18558

--- Comment #3 from Jonathan Marler <johnnymarler at gmail.com> ---
> No. At that point it's an alias of the function. It gets called inside `Foo`:

Ah I see you're right.  If I add pragma(msg, typeof(x)) inside the template, it
shows that x is a function (not just a uint). If I change it to:

Foo!(return42())

Then x will actually be a uint.

Going a bit further, it looks like alias parameters do accept function calls,
but in that case the function call is evaluated outside the template, as
demonstrated by this example:

template Foo(alias x)
{
    pragma(msg, typeof(x));
    enum Foo = x;
}
int return42a() { return 42; }
int return42b() { return 42; }
void main()
{
    auto foo1 = Foo!(return42a());
    auto foo2 = Foo!(return42b());
    auto foo3 = Foo!(42);
}

If you compile this, the template Foo only gets instantiated once meaning that
all 3 of these templates are the same.

Another interesting use case, it looks like you can also use expressions, i.e.

auto foo4 = Foo!(40 + 2);

--


More information about the Digitalmars-d-bugs mailing list