I have the following system of callbacks
class ... {
...
std::vector<std::function<void()>> systemBringUps;
std::vector<std::function<void()>> systemTearDowns;
std::vector<std::function<void(EntityID_t)>> systemMains;
std::vector<std::function<bool(EntityID_t)>> systemChecks;
template <typename T>
void registerSystem() {
systemBringUps.push_back(T::systemBringUp);
systemTearDowns.push_back(T::systemTearDown);
systemMains.push_back(T::systemMain);
systemChecks.push_back(T::systemCheck);
T::onSystemRegister();
}
template <typename T>
void deregisterSystem() {
std::function<void()> bringUp = T::systemBringUp;
std::function<void()> tearDown = T::systemTearDown;
std::function<void(EntityID_t)> smain = T::systemMain;
std::function<bool(EntityID_t)> check = T::systemCheck;
std::remove(systemBringUps.begin(), systemBringUps.end(), bringUp);
std::remove(systemTearDowns.begin(), systemTearDowns.end(), tearDown);
std::remove(systemMains.begin(), systemMains.end(), smain);
std::remove(systemChecks.begin(), systemChecks.end(), check);
T::onSystemDeregister();
}
The registerSystem template function works fine, but the deregisterSystem function fails to compile, stating
...
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/predefined_ops.h:241:17: error: invalid operands to binary expression
('std::function<bool (unsigned short)>' and 'const std::function<bool (unsigned short)>')
{ return *__it == _M_value; }
~~~~~ ^ ~~~~~~~~
...
So it seems that std::remove cannot compile since the std::functions in deregisterSystem that I am defining are const, whereas those in the vectors I am trying to remove from are not? Is there any way I can discard the const qualifier from these std::functions? I have tried using the copy constructor as in
template <typename T>
void deregisterSystem() {
std::function<void()> bringUp(T::systemBringUp);
std::function<void()> tearDown(T::systemTearDown);
std::function<void(EntityID_t)> smain(T::systemMain);
std::function<bool(EntityID_t)> check(T::systemCheck);
std::remove(systemBringUps.begin(), systemBringUps.end(), bringUp);
std::remove(systemTearDowns.begin(), systemTearDowns.end(), tearDown);
std::remove(systemMains.begin(), systemMains.end(), smain);
std::remove(systemChecks.begin(), systemChecks.end(), check);
T::onSystemDeregister();
}
but this fails in a similar way.