You need to provide overrides for functions to use the first arguments N(usually one).
void foo() {
}
template <class First, class... Rest>
void foo(First first, Rest... rest) {
cout << first << endl;
foo(rest...);
}
When you unpack a variable parameter, it finds the next overload.
Example:
foo(42, true, 'a', "hello");
// Calls foo with First = int, and Rest = { bool, char, char* }
// foo(42, Rest = {true, 'a', "hello"}); // not the real syntax
Then, on the next level, we expand the previous one Restand get:
foo(true, Rest = { 'a', "hello"});
, Rest , foo() ( ).
,
, std::tuple
template <class... Pack>
void store_pack(Pack... p) {
std::tuple<Pack...> store( p... );
}
.
,
, :
vector<int> reverse(int i) {
vector<int> ret;
ret.push_back(i);
return ret;
}
template <class... R>
vector<int> reverse(int i, R... r) {
vector<int> ret = reverse(r...);
ret.push_back(i);
return ret;
}
int main() {
auto v = reverse(1, 2, 3, 4);
for_each(v.cbegin(), v.cend(),
[](int i ) {
std::cout << i << std::endl;
}
);
}
.