传递并调用成员函数(boost::bind / boost::function?)
我有一个可能非常简单的问题:传递并调用类中的成员函数。我知道我想使用 BOOST 绑定(和/或函数),但我还没有真正掌握它的概念。
以下代码编译并执行有问题。但是,当我想将“f3”函数更改为非静态类函数时,乐趣就开始了:
#include <iostream>
#include <inttypes.h>
#include <boost/bind.hpp>
#include <boost/function.hpp>
class Test
{
public:
void f1();
private:
void f2(void (*callfunc)(uint32_t));
static void f3(uint32_t x);
};
void Test::f1(){
f2(f3);
}
void Test::f2(void (*callfunc)(uint32_t)){
(*callfunc)(42);
}
void Test::f3(uint32_t x){
std::cout << "x: " << x << std::endl;
}
int main(int argc, char ** argv)
{
Test ct;
ct.f1();
return 0;
}
现在,在更改
static void f3(uint32_t x);
为
void f3(uint32_t x);
编译器后,编译器不高兴并告诉我“错误:没有匹配的函数可用于调用'测试: :f2()'"
阅读了许多关于 boost::bind 和 boost::function 的帖子后,我认为我需要更改 f2() 的定义以及 f1() 如何调用 f2() 给出 f3()作为调用的目标,但除此之外...关于 boost::bind 和 boost 函数的每个组合,我尝试的编译失败了。
我需要怎么写这个?作为一个额外的问题:是否有关于 boost::bind 和 boost::function 的简单介绍性读物? BOOST 文档并没有真正帮助我。
B.
I have a probably embarassingly simple problem: pass and call a member function in a class. I know I want to use BOOST bind (and or function), but I haven't really grasped the concept to it yet.
The following code compiles and executes with problem. But when I want to change the "f3" function to a non-static class function, then the fun begins:
#include <iostream>
#include <inttypes.h>
#include <boost/bind.hpp>
#include <boost/function.hpp>
class Test
{
public:
void f1();
private:
void f2(void (*callfunc)(uint32_t));
static void f3(uint32_t x);
};
void Test::f1(){
f2(f3);
}
void Test::f2(void (*callfunc)(uint32_t)){
(*callfunc)(42);
}
void Test::f3(uint32_t x){
std::cout << "x: " << x << std::endl;
}
int main(int argc, char ** argv)
{
Test ct;
ct.f1();
return 0;
}
Now, after changing
static void f3(uint32_t x);
to
void f3(uint32_t x);
the compiler isn't happy and tells me "error: no matching function for call to 'Test::f2()'"
Having read through a number of SO posts regarding boost::bind and boost::function, I think I need to change the definition of f2() and how f1() calls f2() giving f3() as target to call, but apart from that ... about every combination of boost::bind and boost function I tried miserably fails to compile.
How do I need to write this? As a bonus question: are there any simple introductory reads on boost::bind and boost::function? The BOOST docs did not really help me there.
B.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
boost::function 是一个模板类,它采用函数签名。您还可以使用 function0、function1 等。
boost::function< void(uint32_t) >
定义了一个看起来像函数的“可调用”,即它采用
uint32_t
类型的单个参数并返回 void。适当的编号模板是
function1<;无效,uint32_t>
。这些总是首先指示返回类型,然后按顺序指示参数。boost::bind
是一个非常特殊的函数,它会推导您传递给它的参数并为您创建一个函子。它不会为您创建一个 void(uint32_t) ,它会创建具有 1 模式的东西。
因此,将您的签名更改为:
然后您可以这样调用它:
请注意,奇怪的 _1 是一个“占位符”,告诉 boost::bind 需要将参数放入何处,在本例中为
uint32_t
boost::function is a template class, that takes a function signature. You can also use function0, function1, etc.
boost::function< void(uint32_t) >
defines a "callable" that looks like a function, i.e. it takes a single parameter of type
uint32_t
and returns void.The appropriate numbered template is
function1< void, uint32_t >
. These always indicate the return type first, then the parameters in order.boost::bind
is a very special function that deduces the arguments you pass into it and creates a functor for you.It will not create a void(uint32_t) for you, it will create something that has the pattern of one.
Therefore change your signature to:
Then you can call it like this:
Note the strange _1 is a "placeholder" telling boost::bind where it needs to put in the parameter, in this case the
uint32_t
首先,我将解释删除
static
会导致编译错误的原因:看看这个签名:
这是一个指向带有
uint32_t
的自由函数的指针并返回void
。当f3
在Test
中声明时,f3
是Test
类的成员函数 > 接受uint32_t
并返回void
。因此,没有f3
与f2
所期望的参数类型相匹配。至于如何使用
boost::function
和boost::bind
来提供解决方案:更新:
最后,关于教程:我发现这在学习函子时很有用(这就是
boost::function< /code> 是并且
boost::bind
返回)在过去。它没有具体提到boost
,但是一旦您了解了较低级别到底发生了什么,您就会发现使用boost
轻而易举。First, I 'll explain the reason that removing the
static
gives you a compilation error:Take a look at this signature:
This is a pointer to free function that takes an
uint32_t
and returnsvoid
. Whenf3
is declared insideTest
asthen
f3
is a member function of classTest
that takes anuint32_t
and returnsvoid
. Therefore, there is nof3
that matches the type of the argument thatf2
is expecting.As for how
boost::function
andboost::bind
can be used to provide a solution:Update:
Finally, regarding a tutorial: I found this useful when learning about functors (which is what
boost::function
is andboost::bind
returns) in the past. It doesn't mentionboost
specifically, but once you understand what exactly is going on in a lower level you will find usingboost
a breeze.