评估虚拟堆栈中的可变参数
Evaluate variadic parameters from virtual stack
我正在制作一个基于小堆栈的字节码脚本引擎来学习C++中的嵌入式脚本。目标是能够注册要由脚本调用的任何std::function
。我现在拥有的基本上是
class Bytecode
{
private:
Stack stack;
// Functions to be called from script.
// When a function is called, its arguments are expected to be in the stack.
std::vector<std::function<void(void)> > ops;
public:
// Register C++ function to be called by script
template<typename Func, typename T, typename... Args>
std::size_t function(Func fn, T arg, Args... args)
{
// Substitute value from the stack to function parameter.
auto fn2 = [fn,this](Args ...args) { fn(stack.pop().number, args...); };
return function(fn2, args...);
}
template<typename Func, typename T>
std::size_t function(Func fn, T arg)
{
std::function<void(void)> fn2 = [fn, this]() { fn(stack.pop().number); };
return function(fn2);
}
template<typename Func>
std::size_t function(Func fn)
{
ops.push_back(fn);
// Return bytecode of the function (the same as ops index).
return ops.size() - 1;
}
};
那我就可以做
void myfunc(double a, double b)
{
std::cout << a + b << std::endl;
}
int main()
{
Bytecode bytecode;
// The last two arguments are dummy
auto op = bytecode.function(myfunc, 3.4, 3.6);
}
所以这有效,但我想避免给出虚拟论点。我尝试重载std::size_t function(std::function<void(T, Args...>) fn)
但没有成功,因为似乎专门std::function
模板参数的工作方式与普通模板参数不同。有什么想法吗?
一个解决方案
我最终设法通过反复试验获得了某种有效的解决方案。抱歉,该问题在所需用例上有些模糊。
template<class T, class... Args>
std::size_t function(std::function<void(T,Args...)> &&fn)
{
// Substitute value from the stack to function parameter.
auto fn2 = [fn, this](Args ...args) { fn(stack.pop().as<T>(), std::forward<Args>(args)...); };
return function(std::forward<std::function<void(Args...)> >(fn2));
}
std::size_t function(std::function<void(void)> &&fn)
{
ops.push_back(fn);
// Return bytecode of the function (the same as ops index).
return ops.size() - 1;
}
以及具有不同类型参数的函数的用例
void myfunc(double foo, int bar)
{
std::cout << foo + bar << std::endl;
}
int main()
{
Bytecode bytecode;
auto op = bytecode.function(std::function<void(double,int)>(myfunc));
}
所以函数指针必须用std::function
包裹,但我认为没关系。
函数指针不是std::function
。 您不能(截至 C++14(从函数指针推断std::function
模板的类型。 C++17正在引入这种功能。
一种方法是这样的:
template<typename Func, std::size_t...Is>
std::size_t function(Func fn, std::index_sequence<Is...>) {
std::function<void()> fn2 = [fn, this]() {
// array guarantees left-to-right evaluation:
double elems[] = { ( void(Is), stack.pop().number)... } // comma operator and init list
fn( elems[Is]... );
};
ops.push_back(fn2);
return ops.size()-1;
}
template<std::size_t N, typename Func>
std::size_t function(Func fn) {
return function(fn, std::make_index_sequence<N>{});
}
现在,您只需在调用点指定参数计数:
auto op = bytecode.function<2>(myfunc);
现在,从函数指针中可以推断出类型,但在更一般的情况下,您不能。 在我看来,明确您期望的类型无论如何都是一个好主意。
此解决方案使用 C++14 索引序列并创建索引序列。 每个简短的 C++11 实现都可以在堆栈溢出中找到。
namespace notstd {
template<std::size_T...Is>
struct index_sequence {};
template<std::size_t N, std::size_t...Is>
struct make_index_sequence:
make_index_sequence<N-1, N-1, Is...>
{};
template<std::size_t...Is>
struct make_index_sequence<0, Is...>:
index_sequence<Is...>
{};
}
以上是一个质量相对较低的实现,但对于上面的代码来说已经足够了,在 C++11 中。
相关文章:
- 在不传递参数数量且只有3个点的情况下,如何使用变差函数
- 如何使用可变参数模板强制转换每个变体类型
- 关于如何在具有单个参数的变体构造中选择替代方案?
- 调用参数排列不变函数 f(i++, i++)
- 参数归纳与标准::变体
- 模板化回调参数的逆变,如 C# 中的逆变
- 如何在没有参数包的情况下编写变差函数
- 通过具有嵌套类的工厂类获取多个变异类模板参数包
- 获取模板参数的成员变量值列表
- 保留短 lambda 用作函数的中间参数,使用 clang 格式保持不变
- 如何定义变体<x,y,z>提取模板参数的子类型
- 正确对齐内存模板,参数顺序不变
- 递归中不同参数类型的变元模板函数
- 通过函数指针传递给变差函数的参数会更改其值
- 提升预定义为带有参数的全局 lambda 的变体访问者
- 使用可变参数模板参数提升变体访问者
- boost ::变体 - 为什么模板参数比const字符串参数具有更高的优先级
- 将变参数包中的值加载到临时数组中
- 使用额外参数提升变体访客
- 从变长参数列表中提取std::string