【发布时间】:2018-08-24 04:53:50
【问题描述】:
考虑以下代码:
void func_0()
{
std::cout << "Zero parameter function" << std::endl;
}
void func_1(int i)
{
std::cout << "One parameter function [" << i << "]" << std::endl;
}
void func_2(int i, std::string s)
{
std::cout << "One parameter function [" << i << ", " << s << "]" << std::endl;
}
int main()
{
auto f0 = boost::bind(func_0);
auto f1 = boost::bind(func_1,10);
auto f2 = boost::bind(func_2,20,"test");
f0();
f1();
f2();
}
上面的代码按预期工作。有什么方法可以将 f0、f1、f2 存储在容器中并像这样执行它:
Container cont; // stores all the bound functions.
for(auto func : cont)
{
func();
}
【问题讨论】:
-
这个问题有帮助吗? stackoverflow.com/questions/15128444/…
-
不,那是非常不同的。我这里说的是绑定函数。
-
你使用
boost::bind而不是std::bind有什么原因吗?使用auto表示不再需要使用boost。 -
我可以使用任一绑定。我使用 auto 只是为了方便。
标签: c++ boost-bind