问题描述
可能的重复:
如何将元组扩展为可变参数模板函数争论?
“解包”调用匹配函数指针的元组
在 C++11 模板中,有没有办法使用元组作为(可能是模板)函数的单个参数?
In C++11 templates, is there a way to use a tuple as the individual args of a (possibly template) function?
示例:
假设我有这个功能:
Example:
Let's say I have this function:
我有元组 auto bar = std::make_tuple(1, 2)
.
我可以使用它以模板方式调用 foo(1, 2)
吗?
Can I use that to call foo(1, 2)
in a templaty way?
我的意思不是简单的 foo(std::get<0>(bar), std::get<1>(bar))
因为我想在一个模板中做到这一点不知道参数的数量.
I don't mean simply foo(std::get<0>(bar), std::get<1>(bar))
since I want to do this in a template that doesn't know the number of args.
更完整的例子:
我应该注意,我不希望创建一个适用于一个参数的模板,另一个适用于两个参数等......
I should note that I'd prefer to not create one template that works for one arg, another that works for two, etc…
推荐答案
试试这个:
示例:
通过一些额外的魔法并使用 std::result_of
,您可能还可以使整个事物返回正确的返回值.
With some extra magic and using std::result_of
, you can probably also make the entire thing return the correct return value.
这篇关于C++11:我可以从多个 args 到 tuple,但是我可以从 tuple 到多个 args 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!