Is it possible to use tokens as template parameters?

Steven Schveighoffer schveiguy at gmail.com
Mon Aug 16 19:37:08 UTC 2021


On 8/16/21 1:59 PM, Ali Çehreli wrote:
> On 8/15/21 1:40 AM, Daniel N wrote:
> 
>  > static struct Yay
>  > {
>  >      string opDispatch(string name, T...)(T vals)
> 
> I sometimes wish templates had opDispatch abilities. Then we could 
> instantiate a template with a symbol and the the template could receive 
> it as string just like the way it works for opDispatch. So, this could 
> work:
> 
>    Flag!isCool isCool;
> 
> instead of today's
> 
>    Flag!"isCool" isCool;
> 
> Basically, it would still be a string parameter but the quotes need not 
> be provided:
> 
> template Flag(string tag) {
>    // ...
> }
> 
> I realize that this might hide some bugs but so does opDispatch, no?

Well, aliases do work with operators, so....

```d
import std.typecons;

struct S
{
     alias opDispatch(string foo) = Flag!foo;
}

void main()
{
     S.bar x;
     import std.traits;
     pragma(msg, fullyQualifiedName!(typeof(x))); // 
std.typecons.Flag!("bar")
}
```

-Steve


More information about the Digitalmars-d mailing list