Skip to content

Commit 03a6699

Browse files
author
Paul Dempsey
authored
use cpp in c++ code samples so they get highlighted
1 parent 8abe610 commit 03a6699

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

docs/cpp/template-specialization-cpp.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ translation.priority.ht:
3333
- "zh-tw"
3434
---
3535
# Template Specialization (C++)
36+
3637
Class templates can be partially specialized, and the resulting class is still a template. Partial specialization allows template code to be partially customized for specific types in situations, such as:
3738

3839
- A template has multiple types and only some of them need to be specialized. The result is a template parameterized on the remaining types.
@@ -41,7 +42,7 @@ Class templates can be partially specialized, and the resulting class is still a
4142

4243
## Example
4344

44-
```
45+
```cpp
4546
// partial_specialization_of_class_templates.cpp
4647
template <class T> struct PTS {
4748
enum {
@@ -88,10 +89,11 @@ PTS<S*>::IsPointer == 1 PTS<S*>::IsPointerToDataMember ==0
8889
PTS<int S::*>::IsPointer == 0 PTS<int S::*>::IsPointerToDataMember == 1
8990
```
9091

91-
## Example
92+
## Example
93+
9294
If you have a template collection class that takes any type **T**, you can create a partial specialization that takes any pointer type **T***. The following code demonstrates a collection class template `Bag` and a partial specialization for pointer types in which the collection dereferences the pointer types before copying them to the array. The collection then stores the values that are pointed to. With the original template, only the pointers themselves would have been stored in the collection, leaving the data vulnerable to deletion or modification. In this special pointer version of the collection, code to check for a null pointer in the `add` method is added.
9395

94-
```
96+
```cpp
9597
// partial_specialization_of_class_templates2.cpp
9698
// compile with: /EHsc
9799
#include <iostream>
@@ -199,10 +201,11 @@ Null pointer!
199201
3 87 8 100
200202
```
201203

202-
## Example
204+
## Example
205+
203206
The following example defines a template class that takes pairs of any two types and then defines a partial specialization of that template class specialized so that one of the types is `int`. The specialization defines an additional sort method that implements a simple bubble sort based on the integer.
204207

205-
```
208+
```cpp
206209
// partial_specialization_of_class_templates3.cpp
207210
// compile with: /EHsc
208211
#include <iostream>

0 commit comments

Comments
 (0)