简体   繁体   English

如何制作一个将任意函数指针作为参数的函数?

[英]How can you make a function that takes as a parameter an arbitrary function pointer?

I am trying to do the following, and I am not even sure if it's possible. 我正在尝试执行以下操作,但我不确定是否可行。

I want a function that takes in an arbitrary function pointer to pass it to a different function (I am aware this is a code smell, good software engineering practices are not what I want to discuss right now). 我想要一个带有任意函数指针的函数,以将其传递给另一个函数(我知道这是一种代码味道,良好的软件工程实践不是我现在要讨论的内容)。

In other words what I am looking for would look like: 换句话说,我正在寻找的样子:

void method1(arbitraty pointer p)
{
    method2(p);
}

I am not sure if there is a way to declare arbitrary function pointers (the return value is guaranteed to be void but the parameters are arbitrary, both in number and type) 我不确定是否有一种方法来声明任意函数指针(返回值保证为空,但参数在数量和类型上都是任意的)

Use template and use SFINAE to enable it only if the type deduced is a function pointer type: 仅在推断出的类型为函数指针类型时,才使用模板并使用SFINAE启用它:

template <typename T, std::enable_if_t<std::is_function<T>::value, int> = 0>
void method1(T* p)
{
    // ...
}

This might be considered over-engineering by some, but you could try the following: 有些人可能认为这是过度设计,但您可以尝试以下方法:

Create an enum of each callback that you are interested in: 为您感兴趣的每个回调创建一个枚举:

enum GlfwCallback {
    KeyCallback,
    FramebufferSizeCallback,
    // etc.
};

Then create a type family that associates each of these with the corresponding function pointer type. 然后创建一个类型族,将每个族与相应的函数指针类型相关联。 Do this by creating a template struct and repeatedly specializing it: 通过创建模板结构并反复对其进行专门化来实现:

template<GflwCallback callback>
struct GlfwCallbackType {};

template<>
struct GlfwCallbackType<KeyCallback> {
    using CallbackType = GLFWkeyfun;
    // or
    using CallbackType = void(*)(GLFWwindow *, int, int, int, int);
};

template<>
struct GlfwCallbackType<FramebufferSizeCallback> {
    using CallbackType =  GLFWframebuffersizefun;
};

// etc.

Then you can write 那你可以写

template<GlfwCallback callback>
void method1(GlfwCallbackType<callback>::CallbackType p) {
    // do something with p
    method2<callback>(p);
};

Also, note that you can add other types and even static functions and data members to your "type family" as needed by your application. 另外,请注意,您可以根据应用程序的需要将其他类型,甚至静态函数和数据成员添加到“类型族”中。

A possibility to do what you want, but in a nice, type-safe fashion, would be the use of functors, ie classes that define an overloading for the operator() . 可以使用函子(即定义为operator()定义重载的类)来以理想的,类型安全的方式执行您想要的operator()
Since a functor is a class, you could set the arguments as data memebers, and move the implementation/calling of the functions that you want to pass as arbitrary pointers into the operator() method, where you can have access to all the parameters through the this pointer. 由于函子是一个类,因此您可以将参数设置为数据成员,并将要作为任意指针传递的函数的实现/调用移到operator()方法中,在这里您可以通过以下方式访问所有参数: this指针。

Furthermore, you can define a hierarchy of functors, each one with specialized parameters and implementations, so you can modify the signature of method2 like the following: 此外,您可以定义函子的层次结构,每个函子都有专门的参数和实现,因此您可以像下面这样修改method2的签名:

method2(BaseFunctor* myfunctor) {
   if (myfunctor) 
      (*myfucntor)();
} 

and setup the right type of functor object in the calling context. 并在调用上下文中设置正确的仿函数对象类型。

Also check out lambdas (c++11), that are basically a shortcut to functors definition. 还要检查lambda(c ++ 11),它们基本上是函子定义的快捷方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用任意参数作为模板参数的指针? - How to have a pointer to a function with arbitrary arguments as a template parameter? 默认情况下,如何使函数指针参数为no-op? - How can I make a function pointer parameter no-op by default? 我可以轻松地创建一个模板函数,它接受任意类型的任意容器并对其进行操作吗? - Can I easily make a template function that takes an arbitrary container of an arbitrary type and operates on it? 函数如何将指针返回到带有函数的函数? - How function can return pointer to function that takes a function? 如何制作函数模板,它需要 std::vector 和指向函数的指针? - How to make function template, which takes std::vector and pointer to function? 如何定义一个将任何类型的指针值作为参数的函数? - How can I define a function that takes as a parameter a pointer value of any kind? 将零传递给以指针为参数的函数 - Passing zero to function that takes pointer as parameter 重载的运算符将函数指针作为参数,我该如何检索函数指针的参数 - An overloaded operator takes function pointer as parameter, how do i retrieve arguments of function pointer 如何将成员函数指针传递给带有常规函数指针的函数? - How can I pass a member function pointer into a function that takes a regular function pointer? 执行任意 function 指针 - Execute arbitrary function pointer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM