【问题标题】:Boost bind return type differenceBoost绑定返回类型差异
【发布时间】:2016-09-22 11:49:41
【问题描述】:

我查看了What is the return type of boost::bind? 上的讨论,简短的回答是您不需要知道。

虽然我有一个将“bind(...) result”作为参数的函数,但我发现以下不同的行为

工作案例 1

   void func(int a){};
   myfunc(bind(&obj::func,this,_1));

不工作情况 2(当我想用 2 个参数绑定 func2 时)

   void func2(int a, int b){};
   myfunc(bind(&obj::func2,this,_1,_2));

使其适用于案例 3

   void func3(int a, int b){};
   myfunc(bind(&obj::func3,this,_1, 10));     

所以我的问题是以下 3 的重新运行类型有什么不同?

bind(&obj::func,this,_1));
bind(&obj::func2,this,_1,10)); //why this one can be passed the same type as above one?
bind(&obj::func3,this,_1,_2));

由于myfunc 是一个完全嵌入模板和重载函数的函数,我还没有找到它是如何定义为将“bind(...)”作为参数的。所以我没有附上myfunc的代码

【问题讨论】:

  • 所以你真正的问题是myfunc 的参数应该是什么类型?
  • @SingerOfTheFall ,通过了解bind的返回类型,我可以自己设计我认为想要的myfuc参数类型
  • 你得到什么错误?
  • 你明白std::bind()实际上做了什么吗?
  • “简短的回答是你不需要知道。”...

标签: c++ boost bind


【解决方案1】:

正如您链接的答案所说,您实际上不需要知道确切的返回类型。但是,该类型与boost::function 兼容,因此您可以这样做(作为void(void) 的最简单示例):

typedef boost::function<void(void)> myFunctionType;

// this is the function that you want to bind
void foo(){}
// this is the function that should take it as an argument
void myOtherFunction(myFunctionType f);
<...>
// this is how you bind it
myFunctionType bar = boost::bind(foo);
// and this is how you pass it as argument
myOtherFunction(bar);

【讨论】:

    猜你喜欢
    • 2011-12-19
    • 1970-01-01
    • 2017-05-12
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 2012-03-28
    相关资源
    最近更新 更多