참고 http://kevinushey.github.io/blog/2016/01/27/introduction-to-c++-variadic-templates/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <iostream> using namespace std; void print() { cout << endl; } template <typename T> void print(const T& t) { cout << t << endl; } template <typename First, typename... Rest> void print(const First& first, const Rest&... rest) { cout << first << ", "; print(rest...); // recursive call using pack expansion syntax } int main() { print(); // calls first overload, outputting only a newline print(1); // calls second overload // these call the third overload, the variadic template, // which uses recursion as needed. print(10, 20); print(100, 200, 300); print("first", 2, "third", 3.14159); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 | // The base case: we just have a single number. template <typename T> double sum(T t) { return t; } // The recursive case: we take a number, alongside // some other numbers, and produce their sum. template <typename T, typename... Rest> double sum(T t, Rest... rest) { return t + sum(rest...); } | cs |
'Generic Programming' 카테고리의 다른 글
enable if 사용법 (0) | 2019.03.30 |
---|---|
커리 반대쪽부터 채우기 (0) | 2019.03.30 |
템플릿 예제 (0) | 2019.03.23 |
가변인자 템플릿 예제 (0) | 2019.03.22 |
c++ 람다 표현식과 함수포인터의 모호성 해결법(스택오버플로우) (0) | 2019.02.03 |