传递并调用成员函数(boost::bind / boost::function?)

发布于 2024-10-21 04:03:14 字数 1077 浏览 9 评论 0原文

我有一个可能非常简单的问题:传递并调用类中的成员函数。我知道我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

探春 2024-10-28 04:03:14

boost::function 是一个模板类,它采用函数签名。您还可以使用 function0、function1 等。

boost::function< void(uint32_t) >

定义了一个看起来像函数的“可调用”,即它采用 uint32_t 类型的单个参数并返回 void。

适当的编号模板是 function1<;无效,uint32_t>。这些总是首先指示返回类型,然后按顺序指示参数。

boost::bind 是一个非常特殊的函数,它会推导您传递给它的参数并为您创建一个函子。

它不会为您创建一个 void(uint32_t) ,它会创建具有 1 模式的东西。

因此,将您的签名更改为:

void f2(boost::function<void(uint32_t)>);

然后您可以这样调用它:

f2( boost::bind( &Test::f3, this, _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:

void f2(boost::function<void(uint32_t)>);

Then you can call it like this:

f2( boost::bind( &Test::f3, this, _1 ) );

Note the strange _1 is a "placeholder" telling boost::bind where it needs to put in the parameter, in this case the uint32_t

如梦 2024-10-28 04:03:14

首先,我将解释删除 static 会导致编译错误的原因:

看看这个签名:

void (*callfunc)(uint32_t)

这是一个指向带有 uint32_t 的自由函数的指针并返回void。当 f3Test 中声明时,

void f3(uint32_t x);

f3Test 类的成员函数 > 接受 uint32_t 并返回 void。因此,没有 f3f2 所期望的参数类型相匹配。

至于如何使用 boost::functionboost::bind 来提供解决方案:

void Test::f1(){
    boost::function<void (uint32_t)> f = boost::bind(&Test::f3, this, _1);
    f2(f);
}

更新:

最后,关于教程:我发现在学习函子时很有用(这就是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:

void (*callfunc)(uint32_t)

This is a pointer to free function that takes an uint32_t and returns void. When f3 is declared inside Test as

void f3(uint32_t x);

then f3 is a member function of class Test that takes an uint32_t and returns void. Therefore, there is no f3 that matches the type of the argument that f2 is expecting.

As for how boost::function and boost::bind can be used to provide a solution:

void Test::f1(){
    boost::function<void (uint32_t)> f = boost::bind(&Test::f3, this, _1);
    f2(f);
}

Update:

Finally, regarding a tutorial: I found this useful when learning about functors (which is what boost::function is and boost::bind returns) in the past. It doesn't mention boost specifically, but once you understand what exactly is going on in a lower level you will find using boost a breeze.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文