更新时间:2022-02-25 01:47:28
std :: function
和 std :: result_of
之类的东西使用以下技术关于可变参数模板:
Things like std::function
and std::result_of
use the following technique to do what you want regarding variadic templates:
template<typename Signature>
struct wrapper; // no base template
template<typename Ret, typename... Args>
struct wrapper<Ret(Args...)> {
// instantiated for any function type
};
您可以扩展上述内容以添加非类型 Ret P)(Args ...)
模板参数(指向函数工作的指针),但是你仍然需要一个 decltype
level,即 wrapper< decltype(sin),sin> :: apply
。如果您决定使用宏来删除重复,那么这可能是合法使用预处理器。
You could expand the above to add a non-type Ret(&P)(Args...)
template parameter (pointers to function work just as well) but you'd still need a decltype
at the user level, i.e. wrapper<decltype(sin), sin>::apply
. Arguably it would be a legitimate use of the preprocessor if you decide to use a macro to remove the repetition.
template<typename Sig, Sig& S>
struct wrapper;
template<typename Ret, typename... Args, Ret(&P)(Args...)>
struct wrapper<Ret(Args...), P> {
int
static apply(lua_State*)
{
// pop arguments
// Ret result = P(args...);
// push result & return
return 1;
}
};
// &wrapper<decltype(sin), sin>::apply is your Lua-style wrapper function.
以上编译与gcc-4.5在 ideone 。
运行应用程序的好运气(变化)弹出参数(如果你打开一个问题,给我留言)。你考虑过使用Luabind吗?
The above compiles with gcc-4.5 at ideone. Good luck with implementing the apply that (variadically) pops the arguments (leave me a comment if you open a question about that). Have you considered using Luabind?