考虑以下示例.
#include <iostream>
#include <algorithm>
#include <vector>
#include <boost/bind.hpp>
void
func(int e, int x) {
std::cerr << "x is " << x << std::endl;
std::cerr << "e is " << e << std::endl;
}
struct foo {
std::vector<int> v;
void calc(int x) {
std::for_each(v.begin(), v.end(),
boost::bind(func, _1, x));
}
void func2(int e, int x) {
std::cerr << "x is " << x << std::endl;
std::cerr << "e is " << e << std::endl;
}
};
int
main()
{
foo f; …Run Code Online (Sandbox Code Playgroud) 有人可以用一些简洁的词汇总结如何使用boost shared_from_this<>()智能指针,特别是从使用bind函数在io_service中注册处理程序的角度来看.
编辑:一些回复要求更多背景.基本上,我正在寻找"陷阱",人们使用这种机制观察到的反直觉行为.
请看一下Johannes Schaub发布的这个例子来排序对的向量:
std::sort(a.begin(), a.end(),
boost::bind(&std::pair<int, int>::second, _1) <
boost::bind(&std::pair<int, int>::second, _2));
Run Code Online (Sandbox Code Playgroud)
我以为我确实理解了boost :: bind,但我遇到了这个问题.
问题1:
排序算法期望谓词函数作为第三个参数.我在这里看到的是一个布尔表达式.我错过了什么?:
boost::bind(&std::pair<int, int>::second, _1) < boost::bind(&std::pair<int, int>::second, _2)
Run Code Online (Sandbox Code Playgroud)
对于那两个绑定,boost :: bind库是否重载operator <并且正在返回某种函数指针(如lambda)?
问题2:
这让我感到困惑:
boost::bind(&std::pair<int, int>::second, _1)
Run Code Online (Sandbox Code Playgroud)
通常有一些函数指针作为绑定调用的第一个参数,但是这里是一个类成员的地址?特定绑定的结果是什么?
感谢您的时间和帮助
是否可以使用(boost)bind将参数绑定到函数模板?
// Define a template function (just a silly example)
template<typename ARG1, typename ARG2>
ARG1 FCall2Templ(ARG1 arg1, ARG2 arg2)
{
return arg1 + arg2;
}
// try to bind this template function (and call it)
...
boost::bind(FCall2Templ<int, int>, 42, 56)(); // This works
boost::bind(FCall2Templ, 42, 56)(); // This emits 5 pages of error messages on VS2005
// beginning with: error C2780:
// 'boost::_bi::bind_t<_bi::dm_result<MT::* ,A1>::type,boost::_mfi::dm<M,T>,_bi::list_av_1<A1>::type>
// boost::bind(M T::* ,A1)' : expects 2 arguments - 3 provided
boost::bind<int>(FCall2Templ, …Run Code Online (Sandbox Code Playgroud) 我有一些非常基本的测试代码.我有一个类,只记录它上面的所有操作.我将它绑定到这样的boost::function对象:
void Function(const Foo&)
{
printf("Function invoked\n");
}
// ...
boost::function<void(void)> func;
{
Foo f;
printf("\nConstructing function\n");
func = boost::bind(&Function, f);
printf("Construction complete\n\n");
}
Run Code Online (Sandbox Code Playgroud)
我希望函数对象包含一个副本f.因此,必须至少创建一个副本.但是,我发现我得到了13个临时工.输出是:
Constructing function
Foo::Foo(const Foo&)
Foo::Foo(const Foo&)
Foo::Foo(const Foo&)
Foo::Foo(const Foo&)
Foo::~Foo
Foo::Foo(const Foo&)
Foo::~Foo
Foo::~Foo
Foo::Foo(const Foo&)
Foo::Foo(const Foo&)
Foo::Foo(const Foo&)
Foo::Foo(const Foo&)
Foo::Foo(const Foo&)
Foo::Foo(const Foo&)
Foo::Foo(const Foo&)
Foo::~Foo
Foo::~Foo
Foo::~Foo
Foo::~Foo
Foo::~Foo
Foo::Foo(const Foo&)
Foo::~Foo
Foo::Foo(const Foo&)
Foo::~Foo
Foo::~Foo
Foo::~Foo
Foo::~Foo
Construction complete
Run Code Online (Sandbox Code Playgroud)
我不能使用ref或cref …
有没有一种方法可以部分绑定可调用对象(例如函数)的前/后n个参数,而无需显式指定其余参数?
std::bind()似乎要求所有的参数都被绑定,那些被留下应该绑定到std::placeholders::_1,_2,_3等.
是否有可能从第一个/最后一个参数开始编写bind_first()/ bind_last()用于部分绑定,并且可以在原始位置以原始顺序自动插入任何剩余未绑定参数的占位符?
我在本机C++类中使用boost :: signal,现在我在C++/CLI中编写.NET包装器,这样我就可以将本机C++回调公开为.NET事件.当我尝试使用boost :: bind来获取托管类的成员函数的地址时,我得到编译器错误3374,说我不能获取成员函数的地址,除非我创建一个委托实例.有谁知道如何使用boost :: bind绑定托管类的成员函数?
为了澄清,以下示例代码导致编译器错误3374:
#include <boost/bind.hpp>
public ref class Managed
{
public:
Managed()
{
boost::bind(&Managed::OnSomeEvent, this);
}
void OnSomeEvent(void)
{
}
};
Run Code Online (Sandbox Code Playgroud) 我有一维功能最小化器.现在我正在传递函数指针.然而,许多功能具有多个参数,其中一些参数保持固定.我已经使用像这样的仿函数实现了这个
template <class T>
minimize(T &f) {
}
Functor f(param1, param2);
minimize<Functor>(f);
Run Code Online (Sandbox Code Playgroud)
然而,仿函数定义有很多问题.Boost :: bind看起来更干净.所以我可以这样做:
minimize(boost:bind(f,_1,param1,param2))
Run Code Online (Sandbox Code Playgroud)
但是我不清楚我的minimize声明应该使用什么boost::bind.什么类型的对象是boost::bind?是否有一个简单的模式,避免仿函数的样板,但允许多参数绑定?
我写了一些代码并且害怕它不起作用 - 所以我写了一个原型:
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
class base {
private:
boost::function<void (int)> action;
protected:
virtual void onDataBaseReady(int i) { std::cout << i << std::endl; }
public:
void call() {
action(10);
}
base() {
action = boost::bind(&base::onDataBaseReady, this, _1);
}
};
class child : public base {
protected:
virtual void onDataBaseReady(int i) { std::cout << i+10 << std::endl; }
};
int main()
{
static child c;
c.call();
std::cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译和工作.(输出20).但为什么?我也在VS2010下进行了测试,并想知道它是否适用于跨平台(比如在GCC下编译)?
主要是action = boost::bind(&base::onDataBaseReady, …
我一直认为函数指针不需要&符号:
然而,我所看到的每个使用的例子都boost::bind显示了一个,而我的编译器 - 在大多数情况下 - 如果省略则会给出一个通常难以理解的错误信息.
synchronize(boost::bind(&Device::asyncUpdate , this, "ErrorMessage")); // Works
synchronize(boost::bind(Device::asyncUpdate , this, "ErrorMessage")); // Fails
Run Code Online (Sandbox Code Playgroud)
假设boost::bind第一个参数基本上是函数指针,我错了吗?
boost-bind ×10
c++ ×9
boost ×3
boost-asio ×1
c++-cli ×1
c++11 ×1
currying ×1
delegates ×1
stdbind ×1