Function template advice

Jordan Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 18 14:02:46 PST 2017


I have a simple comparison function:

struct Foo {
     string bar;
}

auto sameGroup(T,S) (T a, S b) {
     static assert (is(T == string) || is(T == Foo));
     static assert (is(S == string) || is(S == Foo));

     string aStr;
     string bStr;
     static if (is(T == string)){
         aStr = a;
     } else {
         aStr = a.bar;
     }
     static if (is(S == string)){
         bStr = b;
     } else {
         bStr = b.bar;
     }
     return equal (aStr,bStr);
}

This works, but just wondered if there was an easier way?

Is there a way to do "static if" in shorthand, like:
auto aStr = (static if (is(T==string)) ? a : a.bar


More information about the Digitalmars-d-learn mailing list