当指针具有可选参数时,如何传递函数指针

How to pass a function pointer when the pointee has optional parameters?

本文关键字:指针 传递函数 参数      更新时间:2023-10-16

我正在尝试将函数指针作为另一个函数的参数传递,但函数指针本身可能有也可能没有参数(使其与我搜索的其他问题不同)。

代码按原样工作,但我的问题是我尝试使用单个函数并传入每个不同的函数指针,但我下面有 3 个不同的函数来传递每个函数指针。我想摆脱 3 个不同的函数定义,因为它们都是一样的,除了传入的函数指针(所以基本上是 execute_func() 定义的 3 个副本)。这是我到目前为止所拥有的,但这似乎不对,我应该需要三个 execute_func() 调用。

class A { ... };
class B { ... };
class Test {
    private:
        std::function<void()> fp;
        std::function<void(MyA &)> fp;
        std::function<void(MyB &)> fp;
    // ...
};
// Here I create a function pointer for each of my calls.
Test::Test() {
    fp = std::bind(&Test::do_this, this);
    fp_a = std::bind(&Test::do_a, this, std::placeholders::_1);
    fp_b = std::bind(&Test::do_b, this, std::placeholders::_1);
}
// Here my intention was to have only 1 execute_func() call and I would 
// pass in the pointer to the function that I want to call.
Test::test_it()
{
    A a;
    B b;
    execute_func(fp);
    execute_func(fp_a, a);
    execute_func(fp_b, b);
}
// I was hoping to only need one function, but so far
// have needed 3 functions with diff signatures to make it work.
bool Test::execute_func(std::function<void()> fp) {
    // ... more code here before the fp call
    fp();
    // ... them more common code here.
}
bool Test::execute_func(std::function<void(MyA &)> fp, MyA &a) { 
    // ... more common code here 
    fp(a);
    // ... and more common code here
}
bool Test::execute_func(std::function<void(MyB &)> fp, MyB &b) {  
    // ... more common code here 
    fp(b);
    // ... and more common code here.
}
// And of course the execute_func() calls call these members as passed in.
bool Test::do_this() { ... }
bool Test::do_a(MyA &a) { ... }
bool Test::do_b(MyB &b) { ... }

关于我哪里出错的想法?

为此,您可以使用可变参数模板。

template<typename Ret, typename... Args>
bool Test::execute_func(Ret fp(Args&&...), Args&&... args)
{
    // do stuff
    fp(args...);
    // do more stuff
}

我在这里使用普通的函数指针而不是 std::function ,这是 IMO、巴洛克式的,完全没有必要(这是C++没有 lambda 时的遗物)。但这是同样的想法。

template<typename Ret, typename... Args>
bool Test::execute_func(const std::function<Ret(Args&&...)> &fp, Args&&... args) { }

编辑:Jarod42指出,你可以做到这一点,这样它就可以与函数指针,std::function和任何其他可调用对象一起工作,如下所示:

template<typename Func, typename... Args>
bool Test::execute_func(Func &&fp, Args&&... args)
{
    // do stuff
    fp(args...);
    // do more stuff
}

为了使它更加通用,您可以使用std::invoke来调用fp而不是直接调用它,这还允许fp成为成员函数或数据成员(在第一个后续参数中给出实例指针)。

template<typename Func, typename... Args>
bool Test::execute_func(Func &&fp, Args&&... args)
{
    // do stuff
    std::invoke(fp, args...);
    // do more stuff
}

前面的例子也可以等效地编写

template<typename... InvokeArgs>
bool Test::execute_func(InvokeArgs&&... iargs)
{
    // do stuff
    std::invoke(iargs...);
    // do more stuff
}