c++ - 如何编写 C++ 类成员函数包装器?

标签 c++ c++11 templates template-meta-programming

我想包装一些类成员函数并围绕它们做一些准备和清理工作。

我尝试复制一些其他线程池代码,但出现一些我无法处理的错误。如何正确操作?

#include <iostream>
#include <string>
using namespace std;
class A {
public:
    void connect() {};
    void close() {};
    template<typename F, typename ... Args>
    auto wrapper(F&& f, Args&& ... args) -> typename std::result_of<F(Args...)>::type {
        using return_type = typename std::result_of<F(Args...)>::type;
        connect();
        return_type ret = f(args...);
        close();
        return ret;
    }
    bool c(int a, string b) {}
    string c(string b) {return b;}
    bool r(int a, string b) {}
};
int main() {
    A a;
    a.connect();
    a.c(1, "abc");
    a.close(); // equal to a.wrapper(a.c, 1, "abc"); but compling error, how to write it correctly?
    cout << "result of a is: " << a.wrapper(a.c, 1, "abc") ? "successful" : "fail" << endl;
    cout << "result of another a is: " << a.wrapper(a.c, "abc") << endl;
    cout << "result of r is:" << a.wrapper(a.r, 1, "abc") << endl;
}

我得到这样的错误:

main.cpp: In function ‘int main()’:
main.cpp:25:58: error: no matching function for call to ‘A::wrapper(<unresolved overloaded function type>, int, const char [4])’
     cout << "result of a is: " << a.wrapper(a.c, 1, "abc") ? "successful" : "fail" << endl;
                                                          ^
main.cpp:25:58: note: candidate is:
main.cpp:9:10: note: template<class F, class ... Args> typename std::result_of<_Functor(_ArgTypes ...)>::type A::wrapper(F&&, Args&& ...)
     auto wrapper(F&& f, Args&& ... args) -> typename std::result_of<F(Args...)>::type {
          ^
main.cpp:9:10: note:   template argument deduction/substitution failed:
main.cpp:25:58: note:   couldn't deduce template parameter ‘F’
     cout << "result of a is: " << a.wrapper(a.c, 1, "abc") ? "successful" : "fail" << endl;
                                                          ^
main.cpp:25:87: error: invalid operands of types ‘const char [5]’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’
     cout << "result of a is: " << a.wrapper(a.c, 1, "abc") ? "successful" : "fail" << endl;
                                                                                       ^
main.cpp:26:63: error: no matching function for call to ‘A::wrapper(<unresolved overloaded function type>, const char [4])’
     cout << "result of another a is: " << a.wrapper(a.c, "abc") << endl;
                                                               ^
main.cpp:26:63: note: candidate is:
main.cpp:9:10: note: template<class F, class ... Args> typename std::result_of<_Functor(_ArgTypes ...)>::type A::wrapper(F&&, Args&& ...)
     auto wrapper(F&& f, Args&& ... args) -> typename std::result_of<F(Args...)>::type {
          ^
main.cpp:9:10: note:   template argument deduction/substitution failed:
main.cpp:26:63: note:   couldn't deduce template parameter ‘F’
     cout << "result of another a is: " << a.wrapper(a.c, "abc") << endl;



                                                                   ^

最佳答案

表达式 a.c 实际上并不是指向可调用成员函数的指针。 &A::c 是,但是它需要调用 A 的实例。

有两种方法可以解决这个问题:

  1. 使用 std::bind .如 std::bind(&A::c, a)

  2. 使用 lambda .如 [&a](int i, std::string const& s) { return a.c(i, s); }

关于c++ - 如何编写 C++ 类成员函数包装器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48355723/

相关文章:

C++ 需要有关 OpenCV 教程中代码的帮助

c++ - 评估为函数的类模板

c++ - 获取连接元组类型;结合 result_of 和 tuple_cat

c++ - 如何在顶部栏中的VS中设置 “Start without debugging”

c++ - 复杂的 C++ 文本文件 IO

c++ - 是否可以强制进行新的模板实例化?

C++11 构造函数重载解析和初始化列表 : clang++ and g++ disagree

C++0x lambdas 编码风格

c++ - 1000个共享指针占用多少内存?

C++ 模板显式右值类型