std::list containing std::function

南楼画角 提交于 2019-12-06 13:16:10

You need to dereference the iterator to get the element it referes to. The iterator itself doesn't have operator().

Write (*f)(state); or, if you wish, f->operator()(state);

You have to get the function element from the iterator before calling with *. Think of it like a sort of "pointer" to the desired value (the std::function object), you have to "dereference" it:

for( std::list<std::function<void(bool)>>::iterator f = m_callbacks_forward.begin(); f != m_callbacks_forward.end(); ++f ){
    (*f)();
}

By the way, if you can use C++11, then take advantage of the for-each loop:

for (std::function<void(bool)> f: m_callbacks_forward) {
    f();
}

or even more concise with auto:

for (auto f: m_callbacks_forward) {
    f();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!