How do you call an eponymous template that has a secondary template arg?

Basile B. b2.temp at gmx.com
Sun Mar 11 13:44:38 UTC 2018


On Sunday, 11 March 2018 at 12:05:56 UTC, aliak wrote:
> Eg:
>
> template aliasOf(T) {
>     enum aliasOf(alias a) = is(typeof(a) == T);
> }
>
> The use case for this is for std.meta.allSatisfy for variadic 
> args, i.e.
>
> template T(values...) if (allSatisfy!(aliasOf!string, values) { 
> ... }
>
> But how do you call that template otherwise?
>
> I've tries:
>
> * aliasOf!int!"string" // multiple ! arguments are not allowed
> * (aliasOf!int)!"string" // error c-style cast
> * aliasOf!int.aliasOf!"string" // template isAliasOf(alias a) 
> does not have property 'isAliasOf
>
> I can work around this by:
>
> template typeOf(T) {
>     enum isAliasedBy(alias a) = is(typeof(a) == T);
> }
>
> and then do:
>
> template T(values...) if 
> (allSatisfy!(typeOf!string.isAliasedBy, values) { ... }
>
> But I like the readability of the former better if there's a 
> way to achieve it?
>
> Cheers
> - Ali

The first version works here:

```
template aliasOf(T) {
     enum aliasOf(alias a) = is(typeof(a) == T);
}

string s;

pragma(msg, allSatisfy!(aliasOf!string, s, "string"));
```

Now on the fact that what is done is correct is another story.
If the literal passed is supposed to be a type then it's clearly 
wrong.
You'd have to mix it:

```
template aliasOf(T)
{
     template aliasOf(alias a)
     {
         mixin("alias A = " ~ a ~ ";");
         enum aliasOf = is(A == T);
     }
}

alias myString1 = string;
alias myString2 = string;

pragma(msg, allSatisfy!(aliasOf!string, "myString1", 
"myString2"));
```


More information about the Digitalmars-d-learn mailing list