先贴个例子看看:
1 #include<iostream> 2 using namespace std; 3 template <int size = 5> 4 void print() 5 { 6 for (int i = 0; i < size; i++) 7 { 8 cout << "aaa" << endl; 9 } 10 } 11 template <typename T> 12 T add(T x, T y) 13 { 14 return x + y; 15 } 16 template <typename M, typename N> 17 void display(M x, N y) 18 { 19 cout << x << endl; 20 cout << y << endl; 21 } 22 template <typename M, int size = 5> 23 void display(M x) 24 { 25 for (int i = 0; i < size; i++) 26 { 27 cout << x << endl; 28 } 29 } 30 int main() 31 { 32 int a = 3, b = 4; 33 float c = 1.2, d = 6.4; 34 cout << add(a, b) << endl; 35 cout << add(c, d) << endl; 36 print<3>(); 37 print(); 38 display(a, c); 39 display<int, 4>(c); 40 return 0; 41 }
<未完。。。>初学勿喷