声明与给定模板参数的函数具有相同签名的函数

Declaring a function with same signature of the given template parameter's function

本文关键字:函数 参数 声明      更新时间:2023-10-16

我正在编写一个要派生的包装类,它隐藏了实现。如何获取给定模板参数函数的签名?

template <class T>
struct wrapper
{
  static typename std::result_of<&T::impl>::type
  call(...) { // this function has the same signature of T::impl();
    // here goes the jobs to do, such as logging or something
    return T::impl(...);
  }
};
struct sum : public wrapper<sum>
{
private:
  friend class wrapper<func>
  static int impl(int a, int b, int c) {
    return a + b + c;
  }
};
int main()
{
  bind_to(&sum::call); // set binding
  std::cout << sum::call(1,2,3) << std::endl;
}

使用参数包:

template <class T>
struct wrapper
{
    template <typename... Args>
    auto call(Args&&... args) -> decltype(T::impl(std::forward<Args>(args)...))
    {
        return T::impl(std::forward<Args>(args)...);
    }
};