使用模板根据类型选择类静态函数

时间:2021-02-23 01:53:41

标签: c++ c++17

模板函数根据类型返回类的静态函数。但是如何使用对象作为推导类型的参数呢?

class A {
 public:
  static funcA() {}
  static funcB() {}
  static funcC() {}
  ...
};

class B {
 public:
  static funcA() {}
  static funcB() {}
  static funcC() {}
  ...
};

using funcType = std::function<void()>

template<typename T>
funcType getfunByType(T t) {
   return T::funcA
}

int main() {
  A a;
  B b;
  // void* p = nullptr; ??
  bool s = false;
  if (s) {
    p = &a;
  } else {
    p = &b;
  }
  // funcType func = getfunByType(&p); ???
  return 0;
}

0 个答案:

没有答案