评估虚拟堆栈中的可变参数

Evaluate variadic parameters from virtual stack

本文关键字:变参 参数 虚拟 堆栈 评估      更新时间:2023-10-16

我正在制作一个基于小堆栈的字节码脚本引擎来学习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 中。