Best way of checking for a templated function instantiation
Arafel via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Aug 10 05:36:14 PDT 2016
Hi,
I'm trying to check at compilation time if a given type
implements some operator (let's assume it's '+' in this case),
without caring about the type of the parameters it accepts. Since
operator overloading is expressed in D through templated
functions, what is the preferred way of checking if a template is
/ can be instantiated with a given parameter list?
So far I've come with a solution using __trait(compiles, ...),
but perhaps it's not 100% reliable -I'm no expert in template
wizardry-, or there are better options. I also tried with
hasMember, but it apparantly only shows that "opBinary" is indeed
present, but nothing more:
---
void main() {
struct S {
int opBinary(string op)(int i) if (op == "+") {
return 0;
}
}
static assert(__traits(compiles, S.opBinary!"+"));
static assert(!__traits(compiles, S.opBinary!"-"));
}
---
More information about the Digitalmars-d-learn
mailing list