[英]How to declare/define a function with the same return and parameter types as a given function pointer?
For example:例如:
int func(const int &i, const int &j);
// Want use pointer to func to do something along these lines.
func::return_type funcHook(func::parameters...)
// So I can have a macro to do this.
HOOK(func) {
// Do hook work.
}
I need to hook over 100 functions and copying and pasting is getting a bit tedious and adds a lot of bloat text.我需要挂接 100 多个函数,复制和粘贴变得有点乏味,并且添加了很多臃肿的文本。
Not sure exactly how you want to use it, but template can do a pretty good job here:不确定你想如何使用它,但模板在这里可以做得很好:
template <auto func> struct Hook;
template <typename Ret, typename ... Args, Ret (*func)(Args...)>
struct Hook
{
static Ret call(Args... args) {
// ...
// return func(std::forward<Args>(args)...);
}
};
// and handle also C-ellipsis as printf
template <typename Ret, typename ... Args, Ret (*func)(Args..., ...)>
struct Hook
{
#if 1
template <typename ...Us>
static Ret call(Args... args, Us&& ... ellipsis_args) {
// ...
// return func(std::forward<Args>(args)..., std::forward<Us>(ellipsis_args)...);
}
#else
static Ret call(Args... args, ...) {
// ...
// Cannot reuse func, as va_args should be used
}
#endif
};
possibly static call
might be replaced by operator ()
.可能
static call
可能被替换为operator ()
。
With usage as用法为
int a = 42, b = 51;
int res = Hook<&func>::call(a, b);
For example:例如:
int func(const int &i, const int &j);
// Want use pointer to func to do something along these lines.
func::return_type funcHook(func::parameters...)
// So I can have a macro to do this.
HOOK(func) {
// Do hook work.
}
I need to hook over 100 functions and copying and pasting is getting a bit tedious and adds a lot of bloat text.我需要挂钩 100 多个函数,复制和粘贴变得有点乏味,并添加了很多臃肿的文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.