从可变参数模板容器类中调用给定可变参数的 in 和 out 方法
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"类头文件: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
不是更好吗?
相关文章:
- 在不传递参数数量且只有3个点的情况下,如何使用变差函数
- 如何使用可变参数模板强制转换每个变体类型
- 关于如何在具有单个参数的变体构造中选择替代方案?
- 调用参数排列不变函数 f(i++, i++)
- 参数归纳与标准::变体
- 模板化回调参数的逆变,如 C# 中的逆变
- 如何在没有参数包的情况下编写变差函数
- 通过具有嵌套类的工厂类获取多个变异类模板参数包
- 获取模板参数的成员变量值列表
- 保留短 lambda 用作函数的中间参数,使用 clang 格式保持不变
- 如何定义变体<x,y,z>提取模板参数的子类型
- 正确对齐内存模板,参数顺序不变
- 递归中不同参数类型的变元模板函数
- 通过函数指针传递给变差函数的参数会更改其值
- 提升预定义为带有参数的全局 lambda 的变体访问者
- 使用可变参数模板参数提升变体访问者
- boost ::变体 - 为什么模板参数比const字符串参数具有更高的优先级
- 将变参数包中的值加载到临时数组中
- 使用额外参数提升变体访客
- 正在将动态数组元素解析为参数?(变音符)