bind作为左值对象
boost::bind as l-value object
是否有办法这样做(MS VS 2008)?
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);
int foo(int){return 0;}
boost::function<int(int)> a = boost::bind(f, _1);
绑定返回未指定的类型,因此您不能直接创建该类型的变量。然而,有一个类型模板boost::function
,它可以为任何函数或函子类型构造。所以:
boost::function<int(int)> a = boost::bind(f, _1);
就可以了。另外,如果您没有绑定任何值,只有占位符,您可以完全不使用bind
,因为function
也可以从函数指针构造。所以:
boost::function<int(int)> a = &f;
只要f
是int f(int)
就可以工作。该类型作为std::function
出现在c++ 11中,用于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
还是很有用的
相关文章:
- 通过 std::bind 从对象成员检索值
- 我可以使用 boost::bind 来存储不相关的对象吗?
- 在另一个函数中使用std::bind返回对象和占位符作为参数
- 如何将 std::bind 对象传递给函数
- c++:如何编写一个类似std::bind的对象来检查超精细的参数
- boost::bind 返回一个函数对象,它是需要指针的函数的参数
- 将具有bind成员函数的std::bind对象传递给函数
- 为什么在使用std::bind和lambda时生成的对象如此之大
- 使用 boost::bind 与模板化对象
- 在对象不存在的情况下,std::bind 中的shared_from_this如何工作
- boost::bind() 一个成员函数,用于提升::函数,对象指针作为占位符
- 使用std::function和std::bind来存储回调并处理对象删除
- 如何使用boost::bind将io_service对象传递给一个新线程
- 是否有可能将boost::bind绑定到已知类但(尚未)未知对象的成员函数?
- bind作为左值对象
- 使用 boost::bind 创建绑定自动释放"heap"资源的函数对象
- 比较Boost.Bind返回的对象
- 提供方法指针和派生类型对象时,std::bind的一致性
- 如何将boost::bind对象存储为成员变量
- 使用std::bind分别绑定参数和对象实例