c++ 可变参数模板

可变参数模板,自己尝试了个例子,如下:

 // variadicTemplates.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "stdio.h"
#include "iostream"
#include "string" using namespace std; template <class... T>
void fun(T... p)
{
cout << sizeof...(p) << endl;
} template <class T>
void fun_print(T t)
{
cout << t << endl;
} template <class T, class ...args>
void fun_print(T head, args... res)
{
cout << head << endl;
fun_print(res...);
} template < typename T >
T fun_sum(T t)
{
return t;
}
template<typename T, typename ...types>
T fun_sum(T head, types ... res)
{
return head + fun_sum(res...);
} int _tmain(int argc, _TCHAR* argv[])
{
fun(, , , , );
fun(1.1, "", );
fun(true, 'a');
fun_print(, true, , 4.9, "");
cout << fun_sum(, , , ) << endl; getchar();
return ;
}
上一篇:Visual Studio 2013 智能提示功能消失解决办法


下一篇:Struts2、spring2、hibernate3在SSH中各起什么作用