Skip to content

Instantly share code, notes, and snippets.

@zhulianhua
Created April 18, 2018 09:17
Show Gist options
  • Save zhulianhua/3d5378cf6e9d5f27a45f15311a807e67 to your computer and use it in GitHub Desktop.
Save zhulianhua/3d5378cf6e9d5f27a45f15311a807e67 to your computer and use it in GitHub Desktop.
C++11 可变参数模板
// 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