I am using std::deque
to keep callback functions.
Everything works perfectly except removing a specific callback.
typedef std::function<void(void)> cb_Action;
std::deque<cb_Action> actionCallbacks;
I can Add items one by one or clear all of them without any problems.
But I cannot remove a specific callback from the deque
variable.
actionCallbacks.erase ( std::remove(actionCallbacks.begin(), actionCallbacks.end(), callbackToRemove), actionCallbacks.end());
It gives compile time error:
binary '==': no operator found which takes a left-hand operand of type:'std::function<void(void)>' (or there is no acceptable conversion)
So, how can I remove a specific cb_Action
?
If you deal with usual functions you can do something like this based on std::function::target :
void callback_1(void) {}
void callback_2(void) {}
actionCallbacks = {
std::function<void(void)>(callback_1),
std::function<void(void)>(callback_2),
std::function<void(void)>(callback_1)
};
actionCallbacks.erase(
std::remove_if(actionCallbacks.begin(), actionCallbacks.end(), [](cb_Action action) {
return (*action.target<void(*)(void)>() == callback_1);
}),
actionCallbacks.end()
);
Here all items with callback_1
inside are removed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.