Last active
July 27, 2017 11:25
-
-
Save odeblic/35cc926e57c69b9665f32d617e24cb5e to your computer and use it in GitHub Desktop.
Simple example using variadic templates
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 <string> | |
#include <iostream> | |
#include <typeinfo> | |
template <typename T> | |
void f(T t) | |
{ | |
std::cout << typeid(t).name() << ":" << t << std::endl; | |
} | |
template <typename T, typename ...N> | |
void f(T t, N... n) | |
{ | |
std::cout << typeid(t).name() << ":" << t << " "; | |
f(n...); | |
} | |
void g() | |
{ | |
std::cout << std::endl; | |
} | |
template <typename T, typename ...N> | |
void g(T t, N... n) | |
{ | |
std::cout << typeid(t).name() << ":" << t << " "; | |
g(n...); | |
} | |
int main() | |
{ | |
f(2); | |
f(1, 2, 3, 4); | |
f("array"); | |
f(333, "foobar"); | |
f(4.5, std::string("text"), 884477ul); | |
std::cout << "======================" << std::endl; | |
g(2); | |
g(1, 2, 3, 4); | |
g("array"); | |
g(333, "foobar"); | |
g(4.5, std::string("text"), 884477ul); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment