Created
June 10, 2015 02:52
-
-
Save Robbepop/5fada43b783c0907e821 to your computer and use it in GitHub Desktop.
Variadic Templates playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
template<typename T> | |
void print_counted_acc(size_t acc, T head) { | |
std::cout << acc << ' ' << head << '\n'; | |
} | |
template<typename T, typename... Args> | |
void print_counted_acc(size_t acc, T head, Args... tail) { | |
std::cout << acc << ' ' << head << '\n'; | |
print_counted_acc(acc + 1, tail...); | |
} | |
template<typename... Args> | |
void print_counted(Args... args) { | |
print_counted_acc(0, args...); | |
} | |
void test() { | |
print_counted(5, 4, -1, 0, 123); | |
print_counted(true, 4, -1, "Hello, World!"); | |
} | |
auto main() -> int { | |
test(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment