1
1
---
2
2
description : " Learn more about: Template Specialization (C++)"
3
3
title : " Template Specialization (C++)"
4
- ms.date : " 11/04/2016 "
4
+ ms.date : 07/09/2021
5
5
helpviewer_keywords : ["partial specialization of class templates"]
6
6
ms.assetid : f3c67c0b-3875-434a-b8d8-bb47e99cf4f0
7
7
---
@@ -17,6 +17,8 @@ Class templates can be partially specialized, and the resulting class is still a
17
17
18
18
``` cpp
19
19
// partial_specialization_of_class_templates.cpp
20
+ #include < stdio.h>
21
+
20
22
template <class T > struct PTS {
21
23
enum {
22
24
IsPointer = 0,
@@ -40,24 +42,25 @@ template <class T, class U> struct PTS<T U::*> {
40
42
41
43
struct S{};
42
44
43
- extern "C" int printf_s(const char* ,...);
44
-
45
45
int main() {
46
- printf_s("PTS<S >::IsPointer == %d PTS <S >::IsPointerToDataMember == %d\n",
46
+ printf_s("PTS<S >::IsPointer == %d \nPTS <S >::IsPointerToDataMember == %d\n",
47
47
PTS<S >::IsPointer, PTS<S >:: IsPointerToDataMember);
48
- printf_s("PTS<S* >::IsPointer == %d PTS <S* >::IsPointerToDataMember ==%d\n"
48
+ printf_s("PTS<S* >::IsPointer == %d \nPTS <S* >::IsPointerToDataMember == %d\n"
49
49
, PTS<S* >::IsPointer, PTS<S* >:: IsPointerToDataMember);
50
- printf_s("PTS<int S::* >::IsPointer == %d PTS "
50
+ printf_s("PTS<int S::* >::IsPointer == %d \nPTS "
51
51
"<int S::* >::IsPointerToDataMember == %d\n",
52
52
PTS<int S::* >::IsPointer, PTS<int S::* >::
53
53
IsPointerToDataMember);
54
54
}
55
55
```
56
56
57
57
```Output
58
- PTS<S>::IsPointer == 0 PTS<S>::IsPointerToDataMember == 0
59
- PTS<S*>::IsPointer == 1 PTS<S*>::IsPointerToDataMember ==0
60
- PTS<int S::*>::IsPointer == 0 PTS<int S::*>::IsPointerToDataMember == 1
58
+ PTS<S>::IsPointer == 0
59
+ PTS<S>::IsPointerToDataMember == 0
60
+ PTS<S*>::IsPointer == 1
61
+ PTS<S*>::IsPointerToDataMember == 0
62
+ PTS<int S::*>::IsPointer == 0
63
+ PTS<int S::*>::IsPointerToDataMember == 1
61
64
```
62
65
63
66
## Example: Partial specialization for pointer types
@@ -159,6 +162,7 @@ int main() {
159
162
xp.add(&j);
160
163
xp.add(p);
161
164
xp.add(p + 1);
165
+ delete[ ] p;
162
166
p = NULL;
163
167
xp.add(p);
164
168
xp.print();
0 commit comments