标签: boost-bind

boost :: function和boost :: bind如何工作

我不喜欢在我的代码中散布着魔术盒......这两个类究竟是如何工作的,基本上允许任何函数映射到一个函数对象,即使函数<>有一个完全不同的参数设置为一个im传递给 boost::bind

它甚至适用于不同的调用约定(即成员方法__thiscall在VC下,但"普通"函数通常__cdecl或者__stdcall需要与C兼容的那些).

c++ boost boost-bind boost-function

82
推荐指数
1
解决办法
6万
查看次数

如何使用成员函数的boost绑定

以下代码导致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)

我究竟做错了什么?

c++ boost boost-bind boost-function

74
推荐指数
1
解决办法
11万
查看次数

C++ 11 std :: bind和boost :: bind之间的区别

这两者有什么区别吗?还是我的安全,以取代所有出现boost::bindstd::bind在我的代码,从而消除对加速的依赖?

c++ boost boost-bind stdbind c++11

66
推荐指数
4
解决办法
2万
查看次数

一般来说,boost绑定如何在幕后工作?

如果不花很长时间来审查boost源代码,有人可以快速了解一下boost bind的实现方式吗?

c++ boost boost-bind

32
推荐指数
1
解决办法
1万
查看次数

boost :: bind与具有引用参数的函数

我注意到,当传递引用参数来提升绑定时,这些参数不会像引用一样.相反,boost会创建该成员的另一个副本,并且传入的原始变量保持不变.

当我更改对指针的引用时,一切正常.

我的问题是:

是否有可能获得工作引用,或者至少在尝试使用引用参数时给出编译错误?

c++ boost-bind

29
推荐指数
2
解决办法
2万
查看次数

boost :: bind的返回类型是什么?

我想将函数的"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)

c++ types boost-bind

25
推荐指数
1
解决办法
8791
查看次数

使用boost :: bind来传递比预期更安全的参数吗?

使用,生成的可能会收到比绑定对象所期望的更多的参数.概念:

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)

c++ boost boost-bind boost-function

24
推荐指数
1
解决办法
5063
查看次数

如何在C++中实现通用回调

原谅我对这个基本问题的无知,但我已经习惯了使用Python,这种事情是微不足道的,我已经完全忘记了如何在C++中尝试这一点.

我希望能够将回调传递给在后台执行缓慢进程的函数,并在进程完成后稍后调用它.此回调可以是自由函数,静态函数或成员函数.我也希望能够在那里注入一些任意的参数用于上下文.(即,在某种程度上实现一个非常差的人的协程.)最重要的是,这个函数将始终采用std :: string,这是进程的输出.我不介意这个参数在最终回调参数列表中的位置是否固定.

我觉得答案将涉及boost :: bind和boost :: function但是我无法确定为了创建任意的callables而需要的精确调用(同时将它们调整为只需要一个字符串),将它们存储在后台进程中,并使用string参数正确调用callable.

c++ boost boost-bind

21
推荐指数
1
解决办法
1万
查看次数

使用函数指针调用虚拟成员函数的基类定义

我想使用成员函数指针调用虚函数的基类实现.

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有某种方法来解决这个问题也会有所帮助.

c++ virtual function-pointers boost-bind

19
推荐指数
1
解决办法
6129
查看次数

为什么std :: bind和boost :: bind不能在这个Boost.Asio教程中互换使用

我在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::threadboost::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)

c++ boost boost-bind boost-asio c++11

18
推荐指数
2
解决办法
6350
查看次数