【问题标题】:std::bind, this and QtConcurrentstd::bind、this 和 QtConcurrent
【发布时间】:2012-11-30 14:39:56
【问题描述】:

我正在尝试使用std::bindthis 绑定到QtConcurrent::blockingMapped 中使用的方法

标题:

class TuringMachine
{
private:
    TRTable table;
    std::set<ConfigNode*> currentConfigs;

    //function object
    std::function<std::set<ConfigNode*>( const TuringMachine*, ConfigNode*)> step_f;
    //method it will hold
    std::set<ConfigNode *> step(TuringMachine* this_m ,ConfigNode *parent);

    std::set<ConfigNode*>& makeStep();

}

来源:

    TuringMachine::TuringMachine(/**/)  
{

    step_f = std::bind(&TuringMachine::step, this, std::placeholders::_1);
}

std::set<ConfigNode*> &TuringMachine::makeStep(){

    auto configSet = QtConcurrent::blockingMapped<QLinkedList<std::set<ConfigNode*>>>(currentConfigs, step_f);//concurrent execution!

   /**/ 
   return currentConfigs;
}
std::set<ConfigNode*> TuringMachine::step(TuringMachine *this_m, ConfigNode * parent){     //the actual step
  /**/
}

所以我在这里做的是在currentConfigs 中的每个ConfigNode 上与blockingMapped 同时运行步骤。我使用std::bindthis 绑定到step,所以它只需要一个参数,如blockingMapped 的文档中所示。

我来了

error: no match for call to '(std::_Bind<std::_Mem_fn<std::set<ConfigNode*> (TuringMachine::*)(TuringMachine*, ConfigNode*)>(TuringMachine*, std::_Placeholder<1>)>) (const TuringMachine*, ConfigNode*)'
.../Qt/474/gcc/include/QtCore/qtconcurrentmapkernel.h:121: error: no match for call to '(std::function<std::set<ConfigNode*>(const TuringMachine*, ConfigNode*)>) (ConfigNode* const&)'
.../Qt/474/gcc/include/QtCore/qtconcurrentmapkernel.h:136: error: no match for call to '(std::function<std::set<ConfigNode*>(const TuringMachine*, ConfigNode*)>) (ConfigNode* const&)'

还有note: 2 arguments expected, 1 provided

我哪里做错了?

编辑

更正的工作版本(供将来“参考”):

标题:

    //function object
    std::function<std::set<ConfigNode*>( ConfigNode*)> step_f;
    //method it will hold
    std::set<ConfigNode *> step(ConfigNode *parent);

来源:

    TuringMachine::TuringMachine(/**/)  
{
    step_f = std::bind(&TuringMachine::step, this, std::placeholders::_1);
}

【问题讨论】:

    标签: c++ c++11 qtconcurrent stdbind


    【解决方案1】:

    如果你想绑定一个成员函数,你必须传递一个 this 指针,这意味着你必须传递 2 个 this-pointers:

    正常调用成员函数:

    struct bar {
      int a;
      void foo() {
        std::cout << a << std::endl;
      }
    
      void call_yourself() {
         auto f = std::bind(&bar::foo, this);
         f();
      }
    };
    

    你的情况:

        step_f = std::bind(&TuringMachine::step, this, this,std::placeholders::_1);
    

    如果不了解您的代码,我可能会重新设计您的代码,这样您就可以避免双 this 指针。

    【讨论】:

    • 为什么需要第二个this
    • @Evgeni 第一个 this 是调用成员函数所必需的,因为这样做时,第二个参数需要是指向您的成员函数源自的类的指针或直接指向您的类的对象。例如,您也可以说std::bind(&amp;foo::bar, bar())
    • 好吧,我基本上尝试的是在多个 ConfigNode 对象上同时运行 stepblockingMapped 似乎是个好主意,因为它就是这样做的。但我不能只使用成员函数,如here 所示。我必须绑定它或使其成为静态/全局。
    • 我消除了双重这个。原来我不需要将它传递给方法。谁知道:-P。现在它的step_f = bind(&amp;TuringMachine::step, this, placeholders::_1); 可以工作了。谢谢
    猜你喜欢
    • 2014-12-17
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    相关资源
    最近更新 更多