使用多个参数调用函数

Call functions with multiple args

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

如果成员函数有不同数量的args,我可以在容器中存储一个成员函数列表,然后稍后调用它们吗。

我觉得我只是错过了一些小东西,但这就是我所走的路。

template<typename T>
class RPCServer
{
public:
    RPCServer(const std::string host, const int port) {}
        // Store the method pointers
    template<typename F> 
    void register_method(const T discriminant, F func) {
        m_callbacks.emplace_back(discriminant,func);
    }
    template<typename... Args>
    void run(T subject, Args... args) {
        auto func = std::find(std::begin(m_callbacks), std::end(m_callbacks), subject);
        if (func != std::end(m_callbacks)) {
            auto res = std::get<1>(*func)(args...); // This doesn't compile 
        }
    }
    ~RPCServer() = default;
private:
        // Store
    std::vector<std::tuple<T, boost::any>> m_callbacks;
};
class Impl
{
public:
    // RPC methods
    void send_data(std::string data) {}
    int get_details(int input) { return 0; }
};

在此处设置

using namespace std::placeholders;
Impl impl;
RPCServer<std::string> server("localhost",1234);
server.register_method("foo", std::bind(&Impl::send_data, impl, _1));
server.register_method("bar", std::bind(&Impl::get_details, impl, _1));
server.run("foo", "blah"s); // This should call  send_data with 'blah' as a arg
auto result = server.run("bar", 1); // Call get_details passing in 1

如何安全地存储/检索一组成员函数类型。

创建适配器模板怎么样?概念验证代码:

#include <iostream>
#include <functional>
template<typename T0, typename... TS> struct FunCaller {
    template<class F> FunCaller(F &&f): f(f) {}
    template<typename... More> T0 operator()(TS &&... as, More &&...) {
            return f(as...);
    }
private:
    std::function<T0(TS...)> f;
};
template<typename T0, typename... TS> inline FunCaller<T0, TS...> funCaller(T0(&&f)(TS...)) { return FunCaller<T0, TS...>(f); }
std::ostream &printSome(std::string const &s1, std::string const &s2) { return std::cout << s1 << ", " << s2 << std::endl; }
int main() {
    auto omg = funCaller(printSome);
    omg("Hello", "world!", "This", "is", "cocaine", "speaking");
}