几种特殊的模板偏特化.
0.基本的偏特化(所有模板参数都特化)1template <typename T1,typename T2>
2class A {};
3
4template <>
5class A <int, int>
6{};
7
2class A {};
3
4template <>
5class A <int, int>
6{};
7
1.部分模板参数特化
1template <typename R, typename Arg1>
2class A
3{};
4
5template <typename Arg1>
6class A <void, Arg1>
7{};
8
9void main()
10{
11 A <int, int> a1;
12 A <void, int> a2;
13}
2. 一种特殊的特化2class A
3{};
4
5template <typename Arg1>
6class A <void, Arg1>
7{};
8
9void main()
10{
11 A <int, int> a1;
12 A <void, int> a2;
13}
1#include <stdio.h>
2
3template<typename Signature> class function;
4
5template<typename R >
6class function<R (void)>
7{
8 public:
9 function(){
10 printf("function<R (void)>\n");
11 }
12};
13
14template<typename R ,
15 typename T0>
16class function<R ( T0)>
17{
18 public:
19 function(){
20 printf("function<R ( T0)>\n");
21 }
22};
23
24 void main()
25 {
26 function <int ()> f0;
27 function <int (int)> f1;
28 }
29
30
2
3template<typename Signature> class function;
4
5template<typename R >
6class function<R (void)>
7{
8 public:
9 function(){
10 printf("function<R (void)>\n");
11 }
12};
13
14template<typename R ,
15 typename T0>
16class function<R ( T0)>
17{
18 public:
19 function(){
20 printf("function<R ( T0)>\n");
21 }
22};
23
24 void main()
25 {
26 function <int ()> f0;
27 function <int (int)> f1;
28 }
29
30