声明具有给定模板参数的函数的相同签名的函数 [英] Declaring a function with same signature of the given template parameter's function

查看:135
本文介绍了声明具有给定模板参数的函数的相同签名的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个要导出的包装类,它隐藏了实现。如何获得给定模板参数的函数的签名?

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


推荐答案

使用参数包: / p>

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

这篇关于声明具有给定模板参数的函数的相同签名的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆