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

aliak something at something.com
Fri Aug 3 16:17:55 UTC 2018


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));

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?

Cheers,
- Ali


More information about the Digitalmars-d-learn mailing list