从可变参数模板容器类中调用给定可变参数的 in 和 out 方法

Call in and out methods given variadic args from a variadic template container class

本文关键字:参数 变参 in 方法 out 容器类 调用      更新时间:2023-10-16

我需要摆脱旧的 c++98 可变参数语法,并使用现代 c++-17 可变参数模板和参数来支持(运行时)函数和解释器函数的进出调用。

我实际上正在尝试...测试它的机制:

template<typename C, typename R, typename ...A>
class methodology{
    std::string _name;
    C* rt_obj = nullptr;
    using rt_fn_t = R(C::*)(A...);
    rt_fn_t rt_fn = nullptr;
    //using out_fn_t  = alu(const alu::list_t& params);
public:
    // `alu` is a custom std::any wrapper container class:
    // Kind of Arithmetic Logical Unit. 
    // teasing js dangerous style
    std::string& name() { return _name; }
    // Runtime calling a given "named" function into the interpreter:
    R operator()(const A& ...args){ 
        // pack into our alu list:        
        auto param = [](auto a){
            return alu(a);
        };
        alu::list_t params = { param(args)...};
        alu a = interpreter::enter(_name, params);
        return a.value<R>();
    }
    /*
       Called from inside the interpreter:
    */
    alu operator()(const alu::list_t& params){
        // Here is my lack of c++ 17 functional knowledges:
        //how to : params => A..args, using this class's typename ...A ???
        return (rt_obj->*rt_fn)(args...);
        return alu(false); // default. Unimplemented.
    }
};

我的问题:(如果需要更多详细信息,请查阅我的"alu"类头文件:https://github.com/bretzel/xio/blob/master/xio%2B%2B/interpreter/kernel/alu.hpp,然后查看实际的旧丑语法:https://github.com/bretzel/xio/blob/master/xio%2B%2B/interpreter/kernel/function_t.hpp)

std::apply(...,std::tuple<>)似乎是要走的路,但是:如何从"alu"列表中构建一个std::tuple<(methodology<typename...A>)>,每个"alu"将参数类型深度地保存到其内部的"std::any"对象中?

不确定您想要什么以及您的alu如何工作,但是...我想你正在寻找以下内容(注意:代码未验证;抱歉)

template <std::size_t ... Is>
alu op_helper (alu::list_t const & params, std::index_sequence<Is...> const &)
 { return (rt_obj->*rt_fn)(params[Is].value<A>()...); }
auto operator() (alu::list_t const & params)
 { return op_helper(params, std::index_sequence_for<A...>{}); }

题外话:鉴于您的alu类包含一组有限且已知的可能类型,std::variant而不是std::any不是更好吗?