1

実装を非表示にする、派生するラッパー クラスを作成しています。指定されたテンプレート パラメーターの関数の署名を取得するにはどうすればよいですか?

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

1 に答える 1

1

パラメーター パックを使用します。

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)...);
    }
};
于 2013-09-05T19:07:27.403 に答える