有没有办法做到这一点(微软VS2008)?
boost::bind mybinder = boost::bind(/*something is binded here*/);
mybinder(/*parameters here*/); // <--- first call
mybinder(/*another parameters here*/); // <--- one more call我试过了
int foo(int){return 0;}
boost::bind<int(*)(int)> a = boost::bind(f, _1);但它不起作用。
发布于 2011-09-16 19:47:54
绑定返回未指定的类型,因此不能直接创建该类型的变量。然而,有一个类型模板boost::function可以为任何函数或函数器类型构造。所以:
boost::function<int(int)> a = boost::bind(f, _1);应该能行得通。另外,如果你不绑定任何值,只绑定占位符,你完全可以不绑定bind,因为function也可以从函数指针构造。所以:
boost::function<int(int)> a = &f;只要f为int f(int),就应该可以工作。该类型作为与C++11闭包一起使用的std::function传递给C++11 (以及bind,这也被接受):
std::function<int(int)> a = [](int i)->int { return f(i, 42); }请注意,对于直接在C++11中调用它,auto的新用法更容易:
auto a = [](int i)->int { return f(i, 42); }但是如果你想把它传递出去,std::function仍然会派上用场。
发布于 2011-09-16 19:47:16
int foo(int){return 0;}
boost::function<int(int)> a = boost::bind(f, _1);https://stackoverflow.com/questions/7444170
复制相似问题