D的有函数

template supportsCall(T, string func) {
  import std.format : format;
  import std.traits : isIntegral;

  enum expr = format!q{
    enum supportsCall = isIntegral!(typeof(T.init.%s()));
  }(func);

  mixin (expr);
}

class AARL (T, string func = "ndx")
if (supportsCall!(T, func)) {
}

int ndx(int i) {
  return 42;
}

struct S {
  long ndx() {
    return 100;
  }
}

void main() {
  auto a = new AARL!int();
  auto b = new AARL!S();
}
posted @ 2021-08-30 08:25  zjh6  阅读(17)  评论(0)    收藏  举报  来源