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

帅比萌擦擦* 提交于 2019-12-11 09:10:06

问题


I'm writing a wrapper class to be derived which hides the implementation. How can I get the signature of the given template parameter's function?

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;
}

回答1:


Use a parameter pack:

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)...);
    }
};


来源:https://stackoverflow.com/questions/18643989/declaring-a-function-with-same-signature-of-the-given-template-parameters-funct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!