Eponymous template with static if

Timon Gehr timon.gehr at gmx.ch
Tue Aug 15 10:53:24 UTC 2023


On 8/15/23 12:25, sighoya wrote:
> On Monday, 14 August 2023 at 14:49:06 UTC, Paul Backus wrote:
>> On Monday, 14 August 2023 at 03:18:17 UTC, Tejas wrote:
>>>
>>> D has [template 
>>> constraints](https://dlang.org/spec/template.html#template_constraints) that will do that for you(but I don't think it works when you explicitly use `template` keyword):
>>
>> You can use constraints with the `template` keyword. They attach to 
>> the template itself, not the function:
>>
>> ```d
>> template square(T)
>> if (is(T)) // <-- right here
>> {
>>     T square(T param) { return param*param; }
>> }
>> ```
> 
> Can you tell what `is(T)` is doing here?

Here it indeed serves no particular purpose, I think this was just to 
illustrate the syntax.

>  From the docs it seems to require `T` to be existing.
> However, I don't know what this exactly means, as T anyway have to exist.

It checks whether `T` is a valid type. I think currently nobody knows 
what this means, but there is a reference implementation.


More information about the Digitalmars-d mailing list