11---
22description : " Learn more about: Template Specialization (C++)"
33title : " Template Specialization (C++)"
4- ms.date : " 11/04/2016 "
4+ ms.date : 07/09/2021
55helpviewer_keywords : ["partial specialization of class templates"]
66ms.assetid : f3c67c0b-3875-434a-b8d8-bb47e99cf4f0
77---
@@ -17,6 +17,8 @@ Class templates can be partially specialized, and the resulting class is still a
1717
1818``` cpp
1919// partial_specialization_of_class_templates.cpp
20+ #include < stdio.h>
21+
2022template <class T > struct PTS {
2123 enum {
2224 IsPointer = 0,
@@ -40,24 +42,25 @@ template <class T, class U> struct PTS<T U::*> {
4042
4143struct S{};
4244
43- extern "C" int printf_s(const char* ,...);
44-
4545int 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",
4747 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"
4949 , PTS<S* >::IsPointer, PTS<S* >:: IsPointerToDataMember);
50- printf_s("PTS<int S::* >::IsPointer == %d PTS "
50+ printf_s("PTS<int S::* >::IsPointer == %d \nPTS "
5151 "<int S::* >::IsPointerToDataMember == %d\n",
5252 PTS<int S::* >::IsPointer, PTS<int S::* >::
5353 IsPointerToDataMember);
5454}
5555```
5656
5757```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
6164```
6265
6366## Example: Partial specialization for pointer types
@@ -159,6 +162,7 @@ int main() {
159162 xp.add(&j);
160163 xp.add(p);
161164 xp.add(p + 1);
165+ delete[ ] p;
162166 p = NULL;
163167 xp.add(p);
164168 xp.print();
0 commit comments