我不喜欢在我的代码中散布着魔术盒......这两个类究竟是如何工作的,基本上允许任何函数映射到一个函数对象,即使函数<>有一个完全不同的参数设置为一个im传递给 boost::bind
它甚至适用于不同的调用约定(即成员方法__thiscall在VC下,但"普通"函数通常__cdecl或者__stdcall需要与C兼容的那些).
以下代码导致cl.exe崩溃(MS VS2005).
我试图使用boost bind来创建一个调用myclass方法的函数:
#include "stdafx.h"
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <functional>
class myclass {
public:
void fun1() { printf("fun1()\n"); }
void fun2(int i) { printf("fun2(%d)\n", i); }
void testit() {
boost::function<void ()> f1( boost::bind( &myclass::fun1, this ) );
boost::function<void (int)> f2( boost::bind( &myclass::fun2, this ) ); //fails
f1();
f2(111);
}
};
int main(int argc, char* argv[]) {
myclass mc;
mc.testit();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
这两者有什么区别吗?还是我的安全,以取代所有出现boost::bind的std::bind在我的代码,从而消除对加速的依赖?
如果不花很长时间来审查boost源代码,有人可以快速了解一下boost bind的实现方式吗?
我注意到,当传递引用参数来提升绑定时,这些参数不会像引用一样.相反,boost会创建该成员的另一个副本,并且传入的原始变量保持不变.
当我更改对指针的引用时,一切正常.
我的问题是:
是否有可能获得工作引用,或者至少在尝试使用引用参数时给出编译错误?
我想将函数的"binder"保存到变量中,通过利用其运算符重载工具在下面的代码中重复使用它.这是实际做我想要的代码:
#include <boost/bind.hpp>
#include <vector>
#include <algorithm>
#include <iostream>
class X
{
int n;
public:
X(int i):n(i){}
int GetN(){return n;}
};
int main()
{
using namespace std;
using namespace boost;
X arr[] = {X(13),X(-13),X(42),X(13),X(-42)};
vector<X> vec(arr,arr+sizeof(arr)/sizeof(X));
_bi::bind_t<int, _mfi::mf0<int, X>, _bi::list1<arg<1> > > bindGetN = bind(&X::GetN,_1);
cout << "With n =13 : "
<< count_if(vec.begin(),vec.end(),bindGetN == 13)
<< "\nWith |n|=13 : "
<< count_if(vec.begin(),vec.end(),bindGetN == 13 || bindGetN == -13)
<< "\nWith |n|=42 : "
<< count_if(vec.begin(),vec.end(),bindGetN == 42 || bindGetN …Run Code Online (Sandbox Code Playgroud) 使用boost-bind,生成的boost函数可能会收到比绑定对象所期望的更多的参数.概念:
int func() { return 42; }
boost::function<int (int,int,int)> boundFunc = boost::bind(&func);
int answer = boundFunc(1,2,3);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,func()即使其签名表明它不带参数,也会在堆栈上接收1,2和3.
这不同于更典型的使用boost::bind用于局部应用,其中的值是固定的,产生了一定的对象boost::function即需要更少的参数,但是提供正确的参数数调用绑定对象时.
以下代码适用于MSVC++ 2010 SP1.这是一种简化形式的帖子; 原始代码也适用于Linux上的g ++ 4.4.
以下是根据C++标准明确定义的吗?
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
using namespace std;
void func1(int x) { std::cout << "func1(" << x << ")\n"; } // end func1()
void func0() { std::cout << "func0()\n"; } // end func0()
int main(int argc,char* argv[])
{
typedef boost::function<void …Run Code Online (Sandbox Code Playgroud) 原谅我对这个基本问题的无知,但我已经习惯了使用Python,这种事情是微不足道的,我已经完全忘记了如何在C++中尝试这一点.
我希望能够将回调传递给在后台执行缓慢进程的函数,并在进程完成后稍后调用它.此回调可以是自由函数,静态函数或成员函数.我也希望能够在那里注入一些任意的参数用于上下文.(即,在某种程度上实现一个非常差的人的协程.)最重要的是,这个函数将始终采用std :: string,这是进程的输出.我不介意这个参数在最终回调参数列表中的位置是否固定.
我觉得答案将涉及boost :: bind和boost :: function但是我无法确定为了创建任意的callables而需要的精确调用(同时将它们调整为只需要一个字符串),将它们存储在后台进程中,并使用string参数正确调用callable.
我想使用成员函数指针调用虚函数的基类实现.
class Base {
public:
virtual void func() { cout << "base" << endl; }
};
class Derived: public Base {
public:
void func() { cout << "derived" << endl; }
void callFunc()
{
void (Base::*fp)() = &Base::func;
(this->*fp)(); // Derived::func will be called.
// In my application I store the pointer for later use,
// so I can't simply do Base::func().
}
};
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,func的派生类实现将从callFunc中调用.有没有办法可以保存指向Base :: func的成员函数指针,还是我必须以using某种方式使用?
在我的实际应用程序中,我使用boost :: bind在callFunc中创建一个boost :: function对象,我后来用它从程序的另一部分调用func.因此,如果boost :: bind或boost :: function有某种方法来解决这个问题也会有所帮助.
我在Boost.Asio文档中尝试了不同的教程,并试图用C++ 11替换boost组件.但是,我在Timer.5中使用std :: bind时出错了 - 在多线程程序中同步处理程序.这是建议的代码:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer { /* Not relevent here */ };
int main()
{
boost::asio::io_service io;
printer p(io);
boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
io.run();
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试图替换boost::threadby std::thread和boost::bindby std::bind.这是我的代码:
#include <functional>
#include <iostream>
#include <thread>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer { /* Not relevent here */ };
int main() {
boost::asio::io_service io;
printer p(io);
std::thread …Run Code Online (Sandbox Code Playgroud)