Is it possible to do this with a template?

bauss jj_1337 at live.dk
Fri Dec 17 09:57:47 UTC 2021


On Friday, 17 December 2021 at 08:59:19 UTC, rempas wrote:
> On Friday, 17 December 2021 at 08:44:39 UTC, Mitacha wrote:
>>
>> It isn't really about limitation of templates. You're trying 
>> to use mixin template and it's main purpose is to inject 
>> declarations. If you want to replace `is expression` with 
>> template you could use something like this:
>>
>> ```d
>> bool is_same(alias value, T)() {
>>     return is(typeof(value) == T);
>>     }
>>
>> void main() {
>>     int value = 10;
>>     static if (is_same!(value, int)) {
>>         writeln("it is true!");
>>     } else {
>>         writeln("it is false!");
>>     }
>> }
>> ```
>
> Oh! That's nice! I didn't even knew it was possible to create 
> template functions like this! Thanks!
>
>> Personally, I don't see any benefit with replacing that kind 
>> of `is expressions` with templates. Perhaps I'm missing 
>> something :)
>
> The benefits are typing less code and make it more readable and 
> easy on the eyes ;)

You can also do it as a normal template:

```d
template is_same(alias value, T)
{
     enum is_same = is(typeof(value) == T);
}
```


More information about the Digitalmars-d-learn mailing list