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
Updated escapes to switch ** to __. Babelmark 2 https://johnmacfarlane.net/babelmark2/ seems to find these changes acceptable.
Cleaned a little formatting.
Copy file name to clipboardExpand all lines: docs/cpp/partial-ordering-of-function-templates-cpp.md
+61-58Lines changed: 61 additions & 58 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,63 +35,65 @@ translation.priority.ht:
35
35
36
36
# Partial Ordering of Function Templates (C++)
37
37
38
-
Multiple function templates that match the argument list of a function call can be available. C++ defines a partial ordering of function templates to specify which function should be called. The ordering is partial because there can be some templates that are considered equally specialized.
39
-
40
-
The compiler chooses the most specialized template function available from the possible matches. For example, if a function template takes a type **T**, and another function template taking **T*** is available, the **T*** version is said to be more specialized and is preferred over the generic **T** version whenever the argument is a pointer type, even though both would be allowable matches.
41
-
42
-
Use the following process to determine if one function template candidate is more specialized:
43
-
44
-
1. Consider two function templates, T1 and T2.
45
-
46
-
2. Replace the parameters in T1 with a hypothetical unique type X.
47
-
48
-
3. With the parameter list in T1, see if T2 is a valid template for that parameter list. Ignore any implicit conversions.
49
-
50
-
4. Repeat the same process with T1 and T2 reversed.
51
-
52
-
5. If one template is a valid template argument list for the other template, but the converse is not true, then that template is considered to be less specialized than the other template. If both templates using the previous step form valid arguments for each other, then they are considered to be equally specialized, and an ambiguous call will result when attempting to use them.
53
-
54
-
6. Using these rules:
55
-
56
-
1. A template specialization for a specific type is more specialized than one taking a generic type argument.
57
-
58
-
2. A template taking only **T*** is more specialized than one taking only **T**, because a hypothetical type **X*** is a valid argument for a **T** template argument, but **X** is not a valid argument for a **T*** template argument.
59
-
60
-
3.**const T** is more specialized than **T**, because **const X** is a valid argument for a **T** template argument, but **X** is not a valid argument for a **const T** template argument.
61
-
62
-
4.**const T*** is more specialized than **T***, because **const X*** is a valid argument for a **T*** template argument, but **X*** is not a valid argument for a **const T*** template argument.
63
-
64
-
7. The following sample works in Visual C++ .NET 2003 as specified in the standard:
65
-
38
+
Multiple function templates that match the argument list of a function call can be available. C++ defines a partial ordering of function templates to specify which function should be called. The ordering is partial because there can be some templates that are considered equally specialized.
39
+
40
+
The compiler chooses the most specialized template function available from the possible matches. For example, if a function template takes a type __T__, and another function template taking __T\*__ is available, the __T\*__ version is said to be more specialized and is preferred over the generic __T__ version whenever the argument is a pointer type, even though both would be allowable matches.
41
+
42
+
Use the following process to determine if one function template candidate is more specialized:
43
+
44
+
1. Consider two function templates, T1 and T2.
45
+
46
+
2. Replace the parameters in T1 with a hypothetical unique type X.
47
+
48
+
3. With the parameter list in T1, see if T2 is a valid template for that parameter list. Ignore any implicit conversions.
49
+
50
+
4. Repeat the same process with T1 and T2 reversed.
51
+
52
+
5. If one template is a valid template argument list for the other template, but the converse is not true, then that template is considered to be less specialized than the other template. If both templates using the previous step form valid arguments for each other, then they are considered to be equally specialized, and an ambiguous call results when you attempt to use them.
53
+
54
+
6. Using these rules:
55
+
56
+
1. A template specialization for a specific type is more specialized than one taking a generic type argument.
57
+
58
+
2. A template taking only __T\*__ is more specialized than one taking only __T__, because a hypothetical type __X\*__ is a valid argument for a __T__ template argument, but __X__ is not a valid argument for a __T\*__ template argument.
59
+
60
+
3.__const T__ is more specialized than __T__, because __const X__ is a valid argument for a __T__ template argument, but __X__ is not a valid argument for a __const T__ template argument.
61
+
62
+
4.__const T\*__ is more specialized than __T\*__, because __const X\*__ is a valid argument for a __T\*__ template argument, but __X\*__ is not a valid argument for a __const T\*__ template argument.
63
+
64
+
## Example
65
+
66
+
The following sample works as specified in the standard:
67
+
66
68
```cpp
67
-
// partial_ordering_of_function_templates.cpp
68
-
// compile with: /EHsc
69
-
#include<iostream>
70
-
71
-
extern "C" int printf(const char*,...);
72
-
template <classT> voidf(T) {
73
-
printf_s("Less specialized function called\n");
74
-
}
75
-
76
-
template <classT> void f(T*) {
77
-
printf_s("More specialized function called\n");
78
-
}
79
-
80
-
template <classT> void f(const T*) {
81
-
printf_s("Even more specialized function for const T*\n");
82
-
}
83
-
84
-
int main() {
85
-
int i =0;
86
-
const int j = 0;
87
-
int *pi = &i;
88
-
const int *cpi = &j;
89
-
90
-
f(i); // Calls less specialized function.
91
-
f(pi); // Calls more specialized function.
92
-
f(cpi); // Calls even more specialized function.
93
-
// Without partial ordering, these calls would be ambiguous.
94
-
}
69
+
// partial_ordering_of_function_templates.cpp
70
+
// compile with: /EHsc
71
+
#include<iostream>
72
+
73
+
extern "C" int printf(const char*,...);
74
+
template <classT> voidf(T) {
75
+
printf_s("Less specialized function called\n");
76
+
}
77
+
78
+
template <classT> void f(T*) {
79
+
printf_s("More specialized function called\n");
80
+
}
81
+
82
+
template <classT> void f(const T*) {
83
+
printf_s("Even more specialized function for const T*\n");
84
+
}
85
+
86
+
int main() {
87
+
int i =0;
88
+
const int j = 0;
89
+
int *pi = &i;
90
+
const int *cpi = &j;
91
+
92
+
f(i); // Calls less specialized function.
93
+
f(pi); // Calls more specialized function.
94
+
f(cpi); // Calls even more specialized function.
95
+
// Without partial ordering, these calls would be ambiguous.
96
+
}
95
97
```
96
98
97
99
### Output
@@ -102,5 +104,6 @@ More specialized function called
0 commit comments