c++ 可变参数模板

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

 

 1 // variadicTemplates.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "stdio.h"
 6 #include "iostream"
 7 #include "string"
 8 
 9 using namespace std;
10 
11 template <class... T>
12 void fun(T... p)
13 {
14     cout << sizeof...(p) << endl;
15 }
16 
17 template <class T>
18 void fun_print(T t)
19 {
20     cout << t << endl;
21 }
22 
23 template <class T, class ...args>
24 void fun_print(T head, args... res)
25 {
26     cout << head << endl;
27     fun_print(res...);
28 }
29 
30 template < typename T >
31 T fun_sum(T t)
32 {
33     return t;
34 }
35 template<typename T, typename ...types>
36 T fun_sum(T head, types ... res)
37 {
38     return head + fun_sum(res...);
39 }
40 
41 int _tmain(int argc, _TCHAR* argv[])
42 {
43     fun(1, 2, 3, 4, 5);
44     fun(1.1, "123", 6);
45     fun(true, 'a');
46     fun_print(1, true, 3, 4.9, "123");
47     cout << fun_sum(3, 6, 9, 12) << endl;
48     
49     getchar();
50     return 0;
51 }

 

posted @ 2018-09-14 17:09  漆天初晓  阅读(504)  评论(0编辑  收藏  举报