如何存储std ::绑定功能指针?

发布于 2025-02-06 21:31:20 字数 2653 浏览 2 评论 0原文

这是我使用的代码,

#include <functional>
#include <iostream>
#include <thread>

class Context {
private:
  std::thread thread;
  bool running;
  void run() {
    while (running) {
      if (function != nullptr) {
        function();
        function = nullptr;
      }
    }
  }
  void (*function)();

public:
  Context() : running(true) { thread = std::thread(&Context::run, this); }
  ~Context() {
    running = false;
    thread.join();
  }

  template <typename T, typename... Args> void call(T function, Args... args) {
    this->function = std::bind(function, args...);
  }
};

// Here are some test functions
void f1() { std::cout << "f1" << std::endl; }

void f2(int a) { std::cout << "f2(" << a << ")" << std::endl; }

void f3(int a, int b) {
  std::cout << "f3(" << a << ", " << b << ")" << std::endl;
}

int main() {
  Context context;
  context.call(f1);
  context.call(f2, 1);
  context.call(f3, 1, 2);
  return 0;
}

我希望此类创建一个将运行一些功能的线程。 当创建类并停止时,将创建线程 当班级被摧毁时。 为了运行功能,您可以使用 函数要被调用和参数。

f2(2);

但是

context.call(f2, 2);

f3(3, 4);

context.call(f3, 3, 4);

当我尝试编译该文件时,我会遇到错误:

t.cpp: In instantiation of 'void Context::call(T, Args ...) [with T = void (*)(); Args = {}]':
t.cpp:55:15:   required from here
t.cpp:42:31: error: cannot convert 'std::_Bind_helper<false, void (*&)()>::type' to 'void (*)()' in assignment
   42 |     this->function = std::bind(function, args...);
      |                      ~~~~~~~~~^~~~~~~~~~~~~~~~~~~
      |                               |
      |                               std::_Bind_helper<false, void (*&)()>::type
t.cpp: In instantiation of 'void Context::call(T, Args ...) [with T = void (*)(int); Args = {int}]':
t.cpp:56:15:   required from here
t.cpp:42:20: error: cannot convert 'std::_Bind_helper<false, void (*&)(int), int&>::type' to 'void (*)()' in assignment
   42 |     this->function = std::bind(function, args...);
      |     ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t.cpp: In instantiation of 'void Context::call(T, Args ...) [with T = void (*)(int, int); Args = {int, int}]':
t.cpp:57:15:   required from here
t.cpp:42:20: error: cannot convert 'std::_Bind_helper<false, void (*&)(int, int), int&, int&>::type' to 'void (*)()' in assignment

知道此代码不安全并且存在缺陷,但是我在实际实现中使用了静音和安全性测量。此示例只是重现错误的最小值。

Here is the code I am using

#include <functional>
#include <iostream>
#include <thread>

class Context {
private:
  std::thread thread;
  bool running;
  void run() {
    while (running) {
      if (function != nullptr) {
        function();
        function = nullptr;
      }
    }
  }
  void (*function)();

public:
  Context() : running(true) { thread = std::thread(&Context::run, this); }
  ~Context() {
    running = false;
    thread.join();
  }

  template <typename T, typename... Args> void call(T function, Args... args) {
    this->function = std::bind(function, args...);
  }
};

// Here are some test functions
void f1() { std::cout << "f1" << std::endl; }

void f2(int a) { std::cout << "f2(" << a << ")" << std::endl; }

void f3(int a, int b) {
  std::cout << "f3(" << a << ", " << b << ")" << std::endl;
}

int main() {
  Context context;
  context.call(f1);
  context.call(f2, 1);
  context.call(f3, 1, 2);
  return 0;
}

I want this class to create a thread that will run some functions.
The thread will be created when the class is created and will be stopped
when the class is destroyed.
In order to run a function, you can call the Context::call function with the
function to be called and the arguments.

f2(2);

becomes

context.call(f2, 2);

and

f3(3, 4);

becomes

context.call(f3, 3, 4);

However I am getting errors when I try to compile this file :

t.cpp: In instantiation of 'void Context::call(T, Args ...) [with T = void (*)(); Args = {}]':
t.cpp:55:15:   required from here
t.cpp:42:31: error: cannot convert 'std::_Bind_helper<false, void (*&)()>::type' to 'void (*)()' in assignment
   42 |     this->function = std::bind(function, args...);
      |                      ~~~~~~~~~^~~~~~~~~~~~~~~~~~~
      |                               |
      |                               std::_Bind_helper<false, void (*&)()>::type
t.cpp: In instantiation of 'void Context::call(T, Args ...) [with T = void (*)(int); Args = {int}]':
t.cpp:56:15:   required from here
t.cpp:42:20: error: cannot convert 'std::_Bind_helper<false, void (*&)(int), int&>::type' to 'void (*)()' in assignment
   42 |     this->function = std::bind(function, args...);
      |     ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t.cpp: In instantiation of 'void Context::call(T, Args ...) [with T = void (*)(int, int); Args = {int, int}]':
t.cpp:57:15:   required from here
t.cpp:42:20: error: cannot convert 'std::_Bind_helper<false, void (*&)(int, int), int&, int&>::type' to 'void (*)()' in assignment

I know this code is unsafe and has flaws but I used mutexes and security measures in the real implementation. This example is just the minimal to reproduce the error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

折戟 2025-02-13 21:31:20

您可以使用std ::功能。特别是,用void> void(*function)() std :: function&lt; void()&gt;函数如下所示:

class Context {
private:
  //other code here
  
  std::function<void ()> function;

};

工作演示

You can make use of std::function. In particular, replace void (*function)() with std::function<void ()> function as shown below:

class Context {
private:
  //other code here
  
  std::function<void ()> function;

};

Working demo

独孤求败 2025-02-13 21:31:20

我会选择其他一些方法。看看:

#include <functional>
#include <iostream>
#include <thread>
using namespace std;

class Context
{
    thread t;

public:
    template<class Func, class... Args>
    Context(Func func, const Args&... args) : t{ func, cref(args)... } {

    }
    ~Context() {
        if (t.joinable())
            t.join();
    }
};

void f1() {
    cout << "f1" << endl;
}

void f2(int) {
    cout << "f2" << endl;
}

void f3(int, int) {
    cout << "f3" << endl;
}

int main()
{
    Context c1{ f1 };
    Context c2{ f2, 1 };
    Context c3{ f3, 1, 2 };
}

I would choose some other approach. Take a look:

#include <functional>
#include <iostream>
#include <thread>
using namespace std;

class Context
{
    thread t;

public:
    template<class Func, class... Args>
    Context(Func func, const Args&... args) : t{ func, cref(args)... } {

    }
    ~Context() {
        if (t.joinable())
            t.join();
    }
};

void f1() {
    cout << "f1" << endl;
}

void f2(int) {
    cout << "f2" << endl;
}

void f3(int, int) {
    cout << "f3" << endl;
}

int main()
{
    Context c1{ f1 };
    Context c2{ f2, 1 };
    Context c3{ f3, 1, 2 };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文