简体   繁体   English

如何声明/定义一个 function 与给定的 function 指针具有相同的返回和参数类型?

[英]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.

相关问题 #define/macro 返回指向同一文件中给出的 function 的 function 指针 - #define/macro to return a function pointer to a function given in the same file 如何定义一个函数指针,该函数指针具有与给定函数相同的参数并返回值? - How can I define a function pointer that takes the same arguments and return value as a given function? 如何为参考参数定义模板函数和为指针参数定义相同的函数 - How to define a template function for a reference parameter and the same function for a pointer parameter 如何声明一个指向返回类型X和输入参数A的函数的指针? - How to declare a pointer to a function of return type X and input parameter A? 如何声明一个函数返回一个指向函数的指针返回一个指向int [3]的指针 - How to declare a function return a pointer to function return pointer to int[3] 如何将指针作为函数参数返回 - How to return a pointer as a function parameter 在一行中声明和定义函数指针变量 - declare and define function pointer variable in one line 如何在C ++中将指向变量的指针声明为函数的参数? - How to declare a pointer to a variable as a parameter of a function in C++? 如何返回 function 的指针并用作参数? - How to return a pointer of a function and using as a parameter? 如何声明__stdcall函数指针 - How to declare an __stdcall function pointer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM