将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用
Pass param packed args into a std::queue to call with a different 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 毫秒处理一次请求,否则它们将被忽略。我的目标是创建一个包装器,如果我一次发送一堆,它会自动添加延迟。
如果我不能这样做,我的后备,尽管我宁愿这样做只是 sprintf(或等效C++(到一个字符串中,只将字符串存储在队列中并在没有所有参数的情况下调用print()
。
也许是这样的:
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();
}
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 函数调用C++中的参数太少
- 将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用
- 为什么即使我调用参数化构造函数也会调用默认构造函数?
- 调用参数排列不变函数 f(i++, i++)
- uncrustify:如何将多行 C 函数调用的参数组合到一行上?
- 函数调用中的参数过多
- 使用显式模板参数列表和 [temp.arg.explicit]/3 的函数调用的演绎失败
- 调用参数不是原子参数的函数是此代码引发异常的原因吗?
- 如何在C++中获取lua函数作为参数,然后调用它
- x64 函数调用参数推送/移动顺序 (MSVC)
- 函数调用参数列表 (C++) 中的空格
- 以可变类模板作为函数调用参数的函数模板参数推导
- 函数调用参数中的表达式交错到什么粒度
- 函数调用参数前的双哈希值
- 创建一个向量作为函数调用参数
- 函数调用参数中的构造函数样式转换
- 取消对拆分函数调用参数的信任