Created
April 18, 2018 09:17
-
-
Save zhulianhua/3d5378cf6e9d5f27a45f15311a807e67 to your computer and use it in GitHub Desktop.
C++11 可变参数模板
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
// blog : https://zhulianhua.wordpress.com/2018/04/18/c11-variadic-template/ | |
#include <iostream> | |
using namespace std; | |
//递归终止函数 | |
void print() | |
{ | |
cout << "empty" << endl; | |
} | |
//展开函数 | |
template <class T, class...Args> | |
void print(T head, Args... rest) | |
{ | |
cout << "parameter " << head << endl; | |
print(rest...); | |
} | |
template <typename T> | |
void printOne(T a) | |
{ | |
cout << a << endl; | |
} | |
template <typename... Args> | |
void printAll(Args... argsPack) | |
{ | |
int res[] = {(printOne(argsPack), 0)...}; | |
} | |
//size of variadic | |
template <typename...T> | |
void varCount(T...argPack) | |
{ | |
cout << sizeof...(argPack) << endl; | |
} | |
int main(void) | |
{ | |
print(1,2,3,4); | |
varCount(1, 2.0, "aaa", 'a'); | |
printAll(1, 2.0, "aaa", 'a', "ABCD"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment