从可变模板容器中给定可变参数调用传入和传出方法 class
Call in and out methods given variadic args from a variadic template container class
我需要摆脱旧的 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" class 头文件:
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
class 拥有一组有限且已知的可能类型,std::variant
不是比 std::any
更好吗?
我需要摆脱旧的 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" class 头文件: 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
class 拥有一组有限且已知的可能类型,std::variant
不是比 std::any
更好吗?