【发布时间】:2018-01-30 10:51:37
【问题描述】:
我正在尝试从我的代码中删除 boost 库。
我没有 std::function,我正在使用 C++98,而不是 C++11。我仍然需要存储来自不同类的简单void Funcs() 向量。
我正在使用一个简单的模板来获取类和函数的实例。 但我想替换对 boost::function 和 boost::bind 的需求。
class App
{
public:
App();
template<class T>
static void AddLoopFunc(void (T::*func)(), T* instance)
{
loop_funcs.push_back(boost::bind(func, instance));
}
static std::vector< boost::function<void()> > loop_funcs;
};
在循环中添加一个函数
App::AddLoopFunc(&MyClass::Loop, this);
【问题讨论】:
-
this是什么?此电话来自 withingMyClass?请提供minimal reproducible example -
为什么要移除 boost 库?
-
您最终会得到近似于
bind和function的东西(专业化)。除非您的问题与 the license boost 使用有关,否则您可以从适当的标题创建派生作品并使用它 -
只是对一个老问题的更新,但 C++11 STL 现在完全支持 std::function 和 std::bind。大多数编译器都兼容 C++11,并且可以轻松替换 boost::function 和 boost::bind 对应的编译器。
标签: c++ templates function-pointers c++03 boost-function