Can you tell if an template alias parameter is of a specific template?

Steven Schveighoffer schveiguy at gmail.com
Sat Aug 4 12:54:49 UTC 2018


On 8/3/18 12:17 PM, aliak wrote:
> Hi
> 
> Is there a way to tell if an alias is to a template?
> 
> I'm writing some algorithms and I need to distinguish between a binary 
> predicate that provides "less than" and one that provides "equal to" 
> semantics. So I have these two templates:
> 
> template eq(alias pred) {
>      alias eq = pred;
> }
> 
> template lt(alias pred) {
>      alias lt = pred;
> }
> 
> Then in some place:
> 
> static if (is(pred == eq)) {
>    return eq(a, b);
> } else static if (is(pred == lt)) {
>    return !lt(a, b) && !lt(b, a); // equality with less than predicate
> } else {
>    // default assumptions about predicate
> }
> 
> Then I can use it like:
> 
> auto a = S!(eq!((a, b) => a == b)) ;
> auto a = S!(lt!((a, b) => a < b));

Once you have an alias, it's the original thing in all respects. So 
there's no way to get the specific alias that was used. That's not a 
bug, but a feature.

> 
> I've tried std.traits.TemplateOf and __traits(isSame and also variations 
> of typeof and also traits.isInstanceOf. I wonder if I have to 
> parameterize eq and lt over a compile time sequence instead?
> 
> Any tips to get this working?

I'd focus on encoding the lt/gt into a type. Much easier to test types. 
Perhaps a UDA works on an alias? I'm not sure.

-Steve


More information about the Digitalmars-d-learn mailing list