[C++] Template Function _ Any number of parameters

Template Function _ Any number of parameters 

 1 #include<iostream>
 2 #include<cstdarg>
 3 using namespace std;
 4 
 5 template <typename T>
 6 T getResult(int count,...)
 7 {
 8     va_list arg_ptr;// the pointer of arguments list
 9     va_start(arg_ptr, count);// begin from arg_ptr , number = count
10     T sum(0);
11     for (int i = 0; i < count; i++)
12     {
13         sum += va_arg(arg_ptr,T);
14     }
15     va_end(arg_ptr);// end 
16     return sum;
17 }
18 int main()
19 {
20     cout << getResult<int>(3,3,4,5) << endl;
21     system("pause");
22 }

 

posted @ 2015-10-27 11:36  Jonas0608  阅读(140)  评论(0编辑  收藏  举报