c++ - 从可变参数模板容器类中给定可变参数调用传入和传出方法

标签 c++ templates c++17 variadic-templates template-meta-programming

我需要摆脱旧的 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<>)似乎要走的路但是: 我如何构建 std::tuple<(methodology<typename...A>)>从“alu”的列表中,每个“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 更好: :任何?

关于c++ - 从可变参数模板容器类中给定可变参数调用传入和传出方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54116101/

相关文章:

c# - 从 C# 访问 C++ .lib 库

c++ - 无法匹配重载的运算符

c++ - 函数调用中表达式的求值顺序

c++ - Windows 上的 C++17 是否与 ubuntu 上的 C++17 一致?

c++ - 函数中的自动参数类型

c++ - 如何高效处理 3D 体素?

c++ - 从字节数组创建IMFByteStream

c++ - std::vector 不支持右值赋值

c++ - 模板和嵌套类/结构

c# - T4 在 C# 运行时运行或编译 tt 文件