从元组解包参数 [英] Unpacking arguments from tuples
问题描述
So I'm trying to figure out how this works: C++11: I can go from multiple args to tuple, but can I go from tuple to multiple args?
我不明白的黑魔法这块代码片段:
The piece of black magic I do not understand is this code fragment:
f(std::get<N>(std::forward<Tuple>(t))...)
这是 f
里面的表达式,我不明白。
it's the expression inside f
that I don't understand.
将 t
中的内容扩展为参数列表。但是有人可以解释这是怎么做的吗?当我看看 std :: get
( http://en.cppreference.com/w/cpp/utility/tuple/get ),我看不到 N
如何适合.. 。?据我所知,N是一个整数序列。
I understand that the expression somehow unpacks/expands what's inside t
into a list of arguments. But could someone care to explain how this is done? When I look at the definition of std::get
(http://en.cppreference.com/w/cpp/utility/tuple/get), I don't see how N
fits in...? As far as I can tell, N is a sequence of integers.
根据我可以观察到的情况,我假设表达式 E
其中 X
是类型 X1
的序列。 X2
,... Xn
,则表达式将扩展为 E < E X2。 ... E< Xn>
。这是怎么工作的?
Based on what I can observe, I'm assuming that expressions in the form E<X>...
where X
is the sequence of types X1
. X2
, ... Xn
, the expression will be expanded as E<X1>, E<X2> ... E<Xn>
. Is this how it works?
编辑:在这种情况下,N不是一个类型序列,而是整数。但我猜这个语言结构适用于类型和值。
Edit: In this case N is not a sequence of types, but integers. But I'm guessing this language construct applies to both types and values.
推荐答案
我认为@ Xeo的评论总结出来。从C ++ 11标准的14.5.3:
I think that @Xeo's comment summed it up well. From 14.5.3 of the C++11 standard:
包扩展包含一个模式 ,
实例化在列表中产生零个或多个
模式的实例化。
A pack expansion consists of a pattern and an ellipsis, the instantiation of which produces zero or more instantiations of the pattern in a list.
在你的情况下,当你完成递归模板实例化并结束部分专业化时,你有
In your case, by the time you finish with the recursive template instantiation and end up in the partial specialization, you have
f(std::get<N>(std::forward<Tuple>(t))...);
...其中 N
是参数包, c $ c> 2
和 3
)。从上面的标准,模式在这里是
...where N
is parameter pack of four int
s (0
, 1
, 2
, and 3
). From the standardese above, the pattern here is
std::get<N>(std::forward<Tuple>(t))
。
省略号到上述模式导致它被扩展成列表形式的四个实例化,即
The application of the ...
ellipsis to the above pattern causes it to be expanded into four instantiations in list form, i.e.
f(std::get<0>(t), std::get<1>(t), std::get<2>(t), std::get<3>(t));
这篇关于从元组解包参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!