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

我实际上是在尝试...测试其机制:

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::any更好吗?

09-07 07:04