【问题标题】:A transform question when using std::bind and std::function使用 std::bind 和 std::function 时的转换问题
【发布时间】:2022-11-04 15:15:04
【问题描述】:

看下面的代码

queue<function<void()> > tasks;

void add_job(function<void(void*)> func, void* arg) {
    function<void()> f = bind(func, arg)();
    tasks.push( f );
}

func 是我想添加到任务中的函数,而它的参数是 arg。如何使用std::bind 绑定其参数,以便将其分配给function&lt;void()&gt; 的对象

【问题讨论】:

  • 绑定后不要调用该函数。

标签: c++ std-function stdbind


【解决方案1】:

只是绑定它,不要执行它。

function<void()> f = bind(func, arg);
tasks.push( f );

【讨论】:

    猜你喜欢
    • 2021-09-18
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多