[英]How to use std::bind with none static member function?
I'm trying to learn some basic c++11 concepts by implementing some well known design patterns. 我正在尝试通过实现一些众所周知的设计模式来学习一些基本的c ++ 11概念。 Currently I'm stuck at Callables while trying to implement the new Angular2 EventEmitter Pattern in C++. 目前,在尝试用C ++实现新的Angular2 EventEmitter模式时,我陷入了Callables的困境。 So here is the code if the comment stays all is ok: 因此,如果注释保留一切正常,这是代码:
class Controller {
public:
Controller() {
const pattern::EventEmitter<SwitchEvent> listner;
listner.subscribe([this](const SwitchEvent& event) {
if(event.get_state()) {
this->_count++;
std::cout << "count: " << this->_count << std::endl;
}
});
//listner.subscribe(std::bind(&Controller::track, this));
}
private:
int _count = 0;
void track(const SwitchEvent& event) {
if(!event.get_state()) {
this->_count++;
std::cout << "count: " << this->_count << std::endl;
}
}
};
and the output is as expected: 和预期的输出:
$ g++ -std=c++11 -Wall test.cc
$ ./a.out
count: 1
But when I remove the comment I get an Error: 但是,当我删除评论时,我得到一个错误:
test.cc: In constructor ‘Controller::Controller()’:
test.cc:50:62: error: no matching function for call to ‘pattern::EventEmitter<SwitchEvent>::subscribe(std::_Bind_helper<false, void (Controller::*)(const SwitchEvent&), Controller*>::type) const’
listner.subscribe(std::bind(&Controller::track, this));
^
test.cc:50:62: note: candidate is:
In file included from test.cc:1:0:
EventEmitter.hh:16:6: note: void pattern::EventEmitter<T>::subscribe(const std::function<void(const T&)>&) const [with T = SwitchEvent]
void subscribe(const std::function<void(const T&)>& listener) const {
^
EventEmitter.hh:16:6: note: no known conversion for argument 1 from ‘std::_Bind_helper<false, void (Controller::*)(const SwitchEvent&), Controller*>::type {aka std::_Bind<std::_Mem_fn<void (Controller::*)(const SwitchEvent&)>(Controller*)>}’ to ‘const std::function<void(const SwitchEvent&)>&’
You need to provide bind
a placeholder so it knows that it takes an argument: 您需要提供bind
占位符,以便它知道需要一个参数:
listner.subscribe(std::bind(&Controller::track, this, std::placeholders::_1));
Otherwise, it will give you a nullary function object, which you cannot construct your std::function
with. 否则,它将为您提供一个无效的函数对象,您无法使用该对象构造std::function
。
You need to pass through the "second" argument to track()
, which is the event. 您需要将“ second”参数传递给track()
,这是事件。 The first argument you bind to this
, but the second is missing. 您绑定this
的第一个参数,但第二个参数丢失。 So you need something like this: 所以你需要这样的东西:
using namespace std::placeholders;
listner.subscribe(std::bind(&Controller::track, this, _1));
Since you tagged it as C++11 then you would be better of using a lambda: 由于您将其标记为C ++ 11,因此最好使用lambda:
listner.subscribe([this](const SwitchEvent& event) {
if(!event.get_state()) {
_count++;
std::cout << "count: " << _count << std::endl;
}});
Otherwise use a placeholder for the track parameter, event
: 否则,请为track参数event
使用一个占位符:
listner.subscribe(std::bind(&Controller::track, this, std::placeholders::_1));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.