Eponymous template with static if

sighoya sighoya at gmail.com
Wed Aug 9 18:28:38 UTC 2023


On Wednesday, 9 August 2023 at 16:55:04 UTC, Michael Galuza wrote:

> But if we write this:
> ```d
> template square(T) {
>     static if (true) {
>         T square(T t) {
>             return t * t;
>         }
>     }
> }
> }
> ```
> trivial expression `square(3)` won't compiled with message
> `Error: none of the overloads of template main.square are 
> callable using argument types !()(int)`.
>
> Why? What I missed?

Giving it an explicit type make it workable 
[example](https://run.dlang.io/?compiler=dmd&source=import%20std.stdio;%0A%0Atemplate%20square(T)%20%7B%0A%20%20%20%20static%20if%20(true)%20%7B%0A%20%20%20%20%20%20%20%20T%20square(T%20t)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20t%20*%20t;%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%0A%0Avoid%20main()%0A%7B%0A%20%20%20%20%2F%2F%20Let%27s%20get%20going!%0A%20%20%20square!(int)(3);%0A%20%20%20writeln(square!(int)(3));%0A%20%20%20%20%2F%2F%20%22Range%20algorithms%22%20page%20under%20%22Gems%22%0A%7D)

It seems not providing a template argument make it default to 
void.



More information about the Digitalmars-d mailing list