将参数打包的args传递到std :: queue中,以便稍后使用其他函数进行调用 [英] Pass param packed args into a std::queue to call with a different function later

查看:104
本文介绍了将参数打包的args传递到std :: queue中,以便稍后使用其他函数进行调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我早些时候问过一个类似的问题,但没有意识到那还不够具体.

I asked a similar question earlier without realizing that that wasn't quite specific enough.

所以我有这个函数,它必须接受打印函数的所有参数,包括...和所有参数,然后将其放入队列中,稍后再调用实际的打印函数.

So I have this function that has to take in all the arguments of a print function, with the ... and all, and then put it into a queue that will call the actual print function later.

类似的东西:

std::queue<SOMETHING> queue;
template <typename... Params>
void printLater(int a, int b, char* fmt, Params ...args) {
    queue.push(args);
}

template <typename... Params>
void print(int a, int b, char* fmt, Param ...args) {
    //whatever
}

void actuallyPrint() {
    //whatever
    print(queue.pop());
}

上下文:我正在使用一种只能每50毫秒处理一次请求的硬件,否则它们将被忽略.我的目标是创建一个包装器,如果我一次发送一堆包装器,它将自动添加延迟.

Context: I'm working with a piece of hardware that can only handle requests every 50ms or else they're ignored. My goal is to create a wrapper that will automatically add the delays if I send it a bunch at once.

如果我做不到,我的后备,尽管我宁愿这样做,只是将sprintf(或C ++等效项)转换为字符串,仅将字符串存储在队列中,并调用print()而不包含所有的args.

My fallback if I cant do this, although I'd rather do this is just sprintf (or C++ equivalent) into a string only store the string in the queue and call print() without all the args.

推荐答案

类似这样的东西:

std::queue<std::function<void()>> queue;

template <typename... Params>
void printLater(int a, int b, char* fmt, Params ...args) {
    queue.push([=](){ print(a, b, fmt, args...); } );
}

void actuallyPrint() {
    queue.front()();
    queue.pop();
}

这篇关于将参数打包的args传递到std :: queue中,以便稍后使用其他函数进行调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆