Tuesday 15 January 2013

c++ - Expansion with variadic templates -



c++ - Expansion with variadic templates -

this question has reply here:

what rules “…” token in context of variadic templates? 2 answers

what difference between next 3 calls gun function?

template <class... ts> void fun(ts... vs) { gun(a<ts...>::hun(vs)...); gun(a<ts...>::hun(vs...)); gun(a<ts>::hun(vs)...); }

i interested in reply explains 3 calls using specific example.

originally literally answered question, wanted expand provide more thorough explanation of how packs expanded what. how think things anyway.

any pack followed ellipses expanded in place. a<ts...> equivalent a<t1, t2, ..., tn> , hun(vs...) equivalent hun(v1, v2, ..., vn). gets complicated when rather pack followed ellipses ((expr)...). expanded (expr1, expr2, ..., exprn) expri refers original look pack replaced ith version of it. if had hun((vs+1)...), becomes hun(v1+1, v2+1, ..., vn+1). gets more fun expr can contain more 1 pack (as long have same size!). how implement standard perfect forwarding model;

foo(std::forward<args>(args)...)

here expr contains 2 packs (args , args both packs) , expansion "iterates" on both:

foo(std::forward<arg1>(arg1), std::forward<arg2>(arg2), ..., std::forward<argn>(argn));

that reasoning should create possible walk through cases for, say, happens when phone call foo(1, 2, '3').

the first one, gun(a<ts...>::hun(vs)...); expands ts "in place" , there's look expand lastly ellipses, calls:

gun(a<int, int, char>::hun(1), a<int, int, char>::hun(2), a<int, int, char>::hun('3'));

the sec one, gun(a<ts...>::hun(vs...)); expands both packs in place:

gun(a<int, int, char>::hun(1, 2, '3'));

the 3rd one, gun(a<ts>::hun(vs)...), expands both packs @ same time:

gun(a<int>::hun(1), a<int>::hun(2), a<char>::hun('3'));

[update] completeness, gun(a<ts>::hun(vs...)...) call:

gun(a<int>::hun(1, 2, '3'), a<int>::hun(1, 2, '3'), a<char>::hun(1, 2, '3'));

finally, there's 1 lastly case consider go overboard on ellipses:

gun(a<ts...>::hun(vs...)...);

this not compile. expand both ts , vs "in place", don't have packs left expand final ellipses.

c++ templates c++11 variadic-templates

No comments:

Post a Comment