C ++绑定重载成员函数并作为参数传递

时间:2019-05-01 09:48:26

标签: c++ std-function stdbind

我有一个带有两个函数的类,这些函数可以启动和停止一组事物。由于这两个函数是相同的,除了它们最终分别在每件事上分别调用启动或停止函数外,我想重构代码,以便将代码主体移至通用函数,并且我的启动和停止集合将此传递进来一个额外的参数,这是他们必须启动或停止的函数。

自然,在线上有很多std::bind()教程和示例,但是我在这里没有发现任何文章或问题/答案涵盖所有我面临的以下特定限制:< / p>

  • 被绑定的函数是使事情复杂化的成员;它也是非常量的
  • 被绑定的函数也被重载,因此必须正确区分
  • 大多数std::bind()示例都使用auto,但是在这种情况下,我需要知道std::bind()的返回类型,以便将其作为参数传递给action_widgets()
  • 有两个布尔ab,它们实际上是恒定的,可以绑定到函数中,尽管我在这里还没有这样做。一次一件事。

这是我要实现的目标的一个示例:

#include <string>
#include <vector>
#include <functional>

struct Processor {

    using WidgetActionFunction = bool(Processor::*)(const std::string&,
                                                    bool, bool);

    // Function wrapper 
    using WidgetActionWrapper = std::function<bool(Processor&,
                                                   const std::string&, bool, bool)>;

    // These functions in reality are tied heavily to the class and are quite
    // large. They cannot easily be made static or free.
    bool stop_widget(const std::string& key, bool a, bool b) { return true; }
    bool start_widget(const std::string& key, bool a, bool b) { return true; }

    // Just to make life difficult, there are some overloads, which we're not
    // interested in.
    bool stop_widget(int event, bool a, bool b) { return true; }
    bool start_widget(int event, bool a, bool b) { return true; }

    // I created this function because start_widgets() and stop_widgets() were
    // identical except that internally they call start_widget() and stop_widget()
    // respectively. I want the main body of the code to be here and for the
    // appropriate function to be passed in.
    void action_widgets(std::vector<std::string>& widgets,
            bool a, bool b, WidgetActionWrapper& func) {
        std::vector<std::string> valid_widgets;
        valid_widgets.reserve(widgets.size());
        for (const auto& widget : widgets) {
            if (func(*this, widget, a, b)) {  // This is where func() gets invoked.
                valid_widgets.push_back(widget);
            }
        }
        std::swap(widgets, valid_widgets);
    }

    void start_widgets(std::vector<std::string>& widgets, bool a, bool b) {
        WidgetActionWrapper func =
            std::bind(static_cast<WidgetActionFunction>(&Processor::start_widget),
                      this, std::placeholders::_1, a, b); // compilation fails here.
        action_widgets(widgets, a, b, func);
    }

    void stop_widgets(std::vector<std::string>& widgets, bool a, bool b) {
        // Very similar to start_widgets() but calls with bound stop_widget()
        // instead.
    }
};

int main()
{
    return 0;
}

编译时出现以下错误:

error: conversion from ‘std::_Bind_helper<false, bool (Processor::*)(const std::basic_string<char>&, bool, bool), Processor* const, const std::_Placeholder<1>&, bool&, bool&>::type {aka std::_Bind<std::_Mem_fn<bool (Processor::*)(const std::basic_string<char>&, bool, bool)>(Processor*, std::_Placeholder<1>, bool, bool)>}’ to non-scalar type ‘Processor::WidgetActionFunctor {aka std::function<bool(Processor&, const std::basic_string<char>&, bool, bool)>}’ requested

很明显,我的函数包装别名与返回的std::bind()不匹配,但是我哪里出错了?

最后一两个警告:因为这是针对公司客户的,所以我仅限于C ++ 11解决方案(尽管为其他人的利益着想的解决方案),而且,尽管我热衷于使用以下更简单的解决方案lambdas,我让同事们相信这可能同样棘手,无论如何,从技术的角度来看,我很想知道自己做错了什么。

2 个答案:

答案 0 :(得分:4)

我认为这里不需要lambda或std::bind,尤其不需要std::function及其带来的所有开销。您可以简单地使用一个成员函数模板,该模板具有一个指向实际成员函数的指针,以作为模板参数在每个小部件上调用,例如:

struct Processor
{
    bool stop_widget(const std::string& key, bool a, bool b) { return true; }
    bool start_widget(const std::string& key, bool a, bool b) { return true; }

    bool stop_widget(int event, bool a, bool b) { return true; }
    bool start_widget(int event, bool a, bool b) { return true; }

    template <bool (Processor::* func)(const std::string&, bool, bool)>
    void action_widgets(std::vector<std::string>& widgets, bool a, bool b)
    {
        std::vector<std::string> valid_widgets;
        valid_widgets.reserve(widgets.size());
        for (const auto& widget : widgets)
        {
            if ((this->*func)(widget, a, b))
            {
                valid_widgets.push_back(widget);
            }
        }
        std::swap(widgets, valid_widgets);
    }
};

然后

processor.action_widgets<&Processor::start_widget>(widgets, true, false);
processor.action_widgets<&Processor::stop_widget>(widgets, true, false);

live example here

这基本上只会导致编译器为您生成原始的start_widgetsstop_widgets函数,就像您手工编写它们一样,没有额外的运行时开销。由于template参数要求使用正确类型的函数,因此编译器应正确确定要使用哪些重载函数……

答案 1 :(得分:3)

您可以将std::bind视为分配给std::function的前几个参数。

例如,此:

bool(Processor::*)(const std::string&, bool, bool);

// Which is this:
class Processor { bool f(const std::string&, bool, bool); }
decltype(&Processor::f)

被分配给std::function<bool(Processor&, const std::string&, bool, bool)>

将其绑定到Processor&(在您的情况下为*this,例如std::bind(&Processor::f, *this))后,现在应将其分配给std::function<bool(const std::string&, bool, bool)>(因为{{ 1}}摆脱了bind参数)。

这里有两个修复程序。不要绑定:

Processor&

或在绑定后将WidgetActionWrapper func = std::bind(static_cast<WidgetActionFunction>(&Processor::start_widget), *this, std::placeholders::_1, a, b); // compilation fails here. // becomes WidgetActionWrapper func = &Processor::start_widget; 更改为正确的

WidgetActionWrapper
相关问题