简体   繁体   English

提升绑定和提升功能,在向量中存储带参数的函数,然后执行它们

[英]Boost bind and boost function, storing functions with arguments in a vector and then executing them

Sorry for the badly-worded title. 抱歉这个措辞严厉的标题。

I've been looking through the documentation, but I cannot find anything that might solve this problem I have. 我一直在查看文档,但我找不到任何可能解决这个问题的方法。

Basically I want to store several function1<void, void*> , with arguments provided, in a vector, and then execute them at a later stage. 基本上我想在向量中存储几个带有参数的function1<void, void*> ,然后在稍后阶段执行它们。

This is what I want to accomplish: 这就是我想要完成的事情:

typedef boost::function1<void, void*> Task;

Vector<Task> mScheduledTasks;
int MyArg = 5;

void SomeTask(void* arg)
{
    // ....
}

void AddSomeTasks()
{
    // nevermind that MyArg is globally accessible
    for (int i = 0; i<5; i++)
        mScheduledTasks.push_back(boost::bind(&SomeTask, _1), (void*)&MyArg);
}

void ExecuteTask()
{
    Task task = mScheduledTasks.front();
    task();
}

Now executing task() it wants me to pass an argument, but I passed it in AddSomeTasks? 现在执行task()它要我传递一个参数,但是我在AddSomeTasks中传递了它? Why is it not using that? 为什么不使用它? Or I have missunderstod the usage of boost::bind? 或者我怀疑使用boost :: bind?

Thanks 谢谢

your Task type wants an argument, it should have been boost::function0<void> . 你的Task类型需要一个参数,它应该是boost::function0<void> When you bind an argument, the returned (bound) callable object is of arity 0, not 1. 绑定参数时,返回的(绑定的)可调用对象是arity 0,而不是1。

Also, to bind an argument you supply it to the call to boost::bind , the _1 etc are for arguments that are left unbound , not what you want here. 另外,要将提供它的参数绑定到对boost::bind的调用, _1等用于保留未绑定的参数,而不是您想要的参数。

Something like (untested): 像(未经测试)的东西:

typedef boost::function0<void> Task;

Vector<Task> mScheduledTasks;
int MyArg = 5;

void SomeTask(void* arg)
{
    // ....
}

void AddSomeTasks()
{
    // nevermind that MyArg is globally accessible
    for (int i = 0; i<5; i++)
        mScheduledTasks.push_back(boost::bind(&SomeTask, (void*)&MyArg));
}

void ExecuteTask()
{
    Task task = mScheduledTasks.front();
    task();
}

It depends on when you want to pass the argument. 这取决于你想要传递参数的时间。 The push_back call mixes two different notions. push_back调用混合了两种不同的概念。 It's not clear from the message whether you want to pass MyArgs at the time you call bind , in which case you'll get back a function object that takes no arguments and returns void, of if you want to pass MyArgs at the time you execute the task. 从消息中不清楚是否要在调用bind时传递MyArgs ,在这种情况下,如果要在执行时传递MyArgs ,则会返回一个不带参数并返回void的函数对象任务。 For the former, as @ForEveR said, the correct call is 对于前者,正如@ForEveR所说,正确的呼叫是

mScheduledTasks.push_back(boost::bind(&SomeTask, (void*)&MyArg));

and you need to change your typedef for Task so that it takes no arguments. 并且您需要更改Task的typedef,以便它不需要参数。 If you want to pass the argument at the point of the call to the task object, then the push_back call would look like this: 如果要在调用task对象时传递参数,则push_back调用将如下所示:

mScheduledTasks.push_back(boost::bind(&SomeTask, _1));

That will create a function object with a function call operator that takes one argument and returns void. 这将创建一个带有函数调用操作符的函数对象,该操作符接受一个参数并返回void。 Then you'd change the call to task() in ExecuteTask to pass whatever argument you have. 然后,您将在ExecuteTask更改对task()的调用,以传递您拥有的任何参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM