我想写一个函数(C++17,如果这很重要的话),它接受多个可调用对象及其参数,并在循环中执行它们。我还想避免任何中间数据复制。
范例:
假设我们有三个函数f0
、f1
和f2
,它们需要不同数量的输入参数
void f0() {
// Implementation of f0
}
void f1(int a) {
// Implementation of f1
}
void f2(int a, int b) {
// Implementation of f2
}
字符串
我正在寻找一种方法,将上述所有函数与示例输入参数一起传递到类或另一个函数(比如Execute
)。示例接口如下所示:
int main() {
// If Execute is a class
Execute exec({f0}, {f1, 4}, {f2, 1, 2});
// The result of the command above is a sequence of calls
// 1. f0()
// 2. f1(4)
// 3. f2(1, 2)
}
型
我希望避免任何中间数据复制。
那么我的问题是,如何才能做到这一点呢?
3条答案
按热度按时间klh5stk11#
为了完整起见,这里有一个零努力的解决方案。
字符串
a0x5cqrl2#
我提出了以下解决方案。谁能告诉我这是否有效?
字符串
ars1skjm3#
这里有一个工作解决方案,它使用
std::tuple
实现了一个模板化的类,它模仿了您的建议。字符串
注意:我需要remember如何取消元组的移位,并使用
std::apply
调用一个函数,该函数使用std::tuple
中的值作为参数。