You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cpp/function-templates.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ translation.priority.ht:
37
37
# Function Templates
38
38
Class templates define a family of related classes that are based on the type arguments passed to the class upon instantiation. Function templates are similar to class templates but define a family of functions. With function templates, you can specify a set of functions that are based on the same code but act on different types or classes. The following function template swaps two items:
39
39
40
-
```
40
+
```cpp
41
41
// function_templates1.cpp
42
42
template< classT > void MySwap( T& a, T& b ) {
43
43
T c(a);
@@ -48,13 +48,13 @@ int main() {
48
48
}
49
49
```
50
50
51
-
This code defines a family of functions that swap the values of the arguments. From this template, you can generate functions that will swap `int` and **long** types and also user-defined types. `MySwap` will even swap classes if the class's copy constructor and assignment operator are properly defined.
51
+
This code defines a family of functions that swap the values of the arguments. From this template, you can generate functions that will swap **int** and **long** types and also user-defined types. `MySwap` will even swap classes if the class's copy constructor and assignment operator are properly defined.
52
52
53
53
In addition, the function template will prevent you from swapping objects of different types, because the compiler knows the types of the `a` and `b` parameters at compile time.
54
54
55
55
Although this function could be performed by a nontemplated function, using void pointers, the template version is typesafe. Consider the following calls:
56
56
57
-
```
57
+
```cpp
58
58
int j = 10;
59
59
int k = 18;
60
60
CString Hello = "Hello, Windows!";
@@ -66,7 +66,7 @@ MySwap( j, Hello ); //error
66
66
67
67
Explicit specification of the template arguments for a function template is allowed. For example:
68
68
69
-
```
69
+
```cpp
70
70
// function_templates2.cpp
71
71
template<classT> voidf(T) {}
72
72
int main(int j) {
@@ -75,10 +75,10 @@ int main(int j) {
75
75
}
76
76
```
77
77
78
-
When the template argument is explicitly specified, normal implicit conversions are done to convert the function argument to the type of the corresponding function template parameters. In the above example, the compiler will convert (`char j`) to type `int`.
78
+
When the template argument is explicitly specified, normal implicit conversions are done to convert the function argument to the type of the corresponding function template parameters. In the above example, the compiler will convert `char j` to type `int`.
0 commit comments