tmp/tmp0dmy9_kp/{from.md → to.md}
RENAMED
|
@@ -1,76 +1,92 @@
|
|
| 1 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 2 |
|
| 3 |
It is possible to overload function templates so that two different
|
| 4 |
function template specializations have the same type.
|
| 5 |
|
|
|
|
|
|
|
| 6 |
``` cpp
|
| 7 |
-
//
|
| 8 |
template<class T>
|
| 9 |
void f(T*);
|
| 10 |
void g(int* p) {
|
| 11 |
f(p); // calls f<int>(int*)
|
| 12 |
}
|
| 13 |
```
|
| 14 |
|
| 15 |
``` cpp
|
| 16 |
-
//
|
| 17 |
template<class T>
|
| 18 |
void f(T);
|
| 19 |
void h(int* p) {
|
| 20 |
f(p); // calls f<int*>(int*)
|
| 21 |
}
|
| 22 |
```
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
``` cpp
|
| 35 |
template<class T> void f();
|
| 36 |
template<int I> void f(); // OK: overloads the first template
|
| 37 |
// distinguishable with an explicit template argument list
|
| 38 |
```
|
| 39 |
|
|
|
|
|
|
|
| 40 |
When an expression that references a template parameter is used in the
|
| 41 |
function parameter list or the return type in the declaration of a
|
| 42 |
function template, the expression that references the template parameter
|
| 43 |
is part of the signature of the function template. This is necessary to
|
| 44 |
permit a declaration of a function template in one translation unit to
|
| 45 |
be linked with another declaration of the function template in another
|
| 46 |
translation unit and, conversely, to ensure that function templates that
|
| 47 |
are intended to be distinct are not linked with one another.
|
| 48 |
|
|
|
|
|
|
|
| 49 |
``` cpp
|
| 50 |
template <int I, int J> A<I+J> f(A<I>, A<J>); // #1
|
| 51 |
template <int K, int L> A<K+L> f(A<K>, A<L>); // same as #1
|
| 52 |
template <int I, int J> A<I-J> f(A<I>, A<J>); // different from #1
|
| 53 |
```
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
| 60 |
Two expressions involving template parameters are considered
|
| 61 |
*equivalent* if two function definitions containing the expressions
|
| 62 |
-
would satisfy the one
|
| 63 |
the tokens used to name the template parameters may differ as long as a
|
| 64 |
token used to name a template parameter in one expression is replaced by
|
| 65 |
another token that names the same template parameter in the other
|
| 66 |
expression. For determining whether two dependent names ([[temp.dep]])
|
| 67 |
are equivalent, only the name itself is considered, not the result of
|
| 68 |
name lookup in the context of the template. If multiple declarations of
|
| 69 |
the same function template differ in the result of this name lookup, the
|
| 70 |
result for the first declaration is used.
|
| 71 |
|
|
|
|
|
|
|
| 72 |
``` cpp
|
| 73 |
template <int I, int J> void f(A<I+J>); // #1
|
| 74 |
template <int K, int L> void f(A<K+L>); // same as #1
|
| 75 |
|
| 76 |
template <class T> decltype(g(T())) h();
|
|
@@ -79,10 +95,12 @@ template <class T> decltype(g(T())) h() // redeclaration of h() uses the
|
|
| 79 |
{ return g(T()); } // ...although the lookup here does find g(int)
|
| 80 |
int i = h<int>(); // template argument substitution fails; g(int)
|
| 81 |
// was not in scope at the first declaration of h()
|
| 82 |
```
|
| 83 |
|
|
|
|
|
|
|
| 84 |
Two expressions involving template parameters that are not equivalent
|
| 85 |
are *functionally equivalent* if, for any given set of template
|
| 86 |
arguments, the evaluation of the expression results in the same value.
|
| 87 |
|
| 88 |
Two function templates are *equivalent* if they are declared in the same
|
|
@@ -93,11 +111,13 @@ parameters. Two function templates are *functionally equivalent* if they
|
|
| 93 |
are equivalent except that one or more expressions that involve template
|
| 94 |
parameters in the return types and parameter lists are functionally
|
| 95 |
equivalent using the rules described above to compare expressions
|
| 96 |
involving template parameters. If a program contains declarations of
|
| 97 |
function templates that are functionally equivalent but not equivalent,
|
| 98 |
-
the program is ill-formed
|
|
|
|
|
|
|
| 99 |
|
| 100 |
This rule guarantees that equivalent declarations will be linked with
|
| 101 |
one another, while not requiring implementations to use heroic efforts
|
| 102 |
to guarantee that functionally equivalent declarations will be treated
|
| 103 |
as distinct. For example, the last two declarations are functionally
|
|
@@ -115,5 +135,7 @@ template <int I> void f(A<I>, A<I+11>);
|
|
| 115 |
// Ill-formed, no diagnostic required
|
| 116 |
template <int I> void f(A<I>, A<I+10>);
|
| 117 |
template <int I> void f(A<I>, A<I+1+2+3+4>);
|
| 118 |
```
|
| 119 |
|
|
|
|
|
|
|
|
|
| 1 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 2 |
|
| 3 |
It is possible to overload function templates so that two different
|
| 4 |
function template specializations have the same type.
|
| 5 |
|
| 6 |
+
[*Example 1*:
|
| 7 |
+
|
| 8 |
``` cpp
|
| 9 |
+
// translation unit 1:
|
| 10 |
template<class T>
|
| 11 |
void f(T*);
|
| 12 |
void g(int* p) {
|
| 13 |
f(p); // calls f<int>(int*)
|
| 14 |
}
|
| 15 |
```
|
| 16 |
|
| 17 |
``` cpp
|
| 18 |
+
// translation unit 2:
|
| 19 |
template<class T>
|
| 20 |
void f(T);
|
| 21 |
void h(int* p) {
|
| 22 |
f(p); // calls f<int*>(int*)
|
| 23 |
}
|
| 24 |
```
|
| 25 |
|
| 26 |
+
— *end example*]
|
| 27 |
+
|
| 28 |
+
Such specializations are distinct functions and do not violate the
|
| 29 |
+
one-definition rule ([[basic.def.odr]]).
|
| 30 |
+
|
| 31 |
+
The signature of a function template is defined in Clause
|
| 32 |
+
[[intro.defs]]. The names of the template parameters are significant
|
| 33 |
+
only for establishing the relationship between the template parameters
|
| 34 |
+
and the rest of the signature.
|
| 35 |
+
|
| 36 |
+
[*Note 1*:
|
| 37 |
+
|
| 38 |
+
Two distinct function templates may have identical function return types
|
| 39 |
+
and function parameter lists, even if overload resolution alone cannot
|
| 40 |
+
distinguish them.
|
| 41 |
|
| 42 |
``` cpp
|
| 43 |
template<class T> void f();
|
| 44 |
template<int I> void f(); // OK: overloads the first template
|
| 45 |
// distinguishable with an explicit template argument list
|
| 46 |
```
|
| 47 |
|
| 48 |
+
— *end note*]
|
| 49 |
+
|
| 50 |
When an expression that references a template parameter is used in the
|
| 51 |
function parameter list or the return type in the declaration of a
|
| 52 |
function template, the expression that references the template parameter
|
| 53 |
is part of the signature of the function template. This is necessary to
|
| 54 |
permit a declaration of a function template in one translation unit to
|
| 55 |
be linked with another declaration of the function template in another
|
| 56 |
translation unit and, conversely, to ensure that function templates that
|
| 57 |
are intended to be distinct are not linked with one another.
|
| 58 |
|
| 59 |
+
[*Example 2*:
|
| 60 |
+
|
| 61 |
``` cpp
|
| 62 |
template <int I, int J> A<I+J> f(A<I>, A<J>); // #1
|
| 63 |
template <int K, int L> A<K+L> f(A<K>, A<L>); // same as #1
|
| 64 |
template <int I, int J> A<I-J> f(A<I>, A<J>); // different from #1
|
| 65 |
```
|
| 66 |
|
| 67 |
+
— *end example*]
|
| 68 |
+
|
| 69 |
+
[*Note 2*: Most expressions that use template parameters use non-type
|
| 70 |
+
template parameters, but it is possible for an expression to reference a
|
| 71 |
+
type parameter. For example, a template type parameter can be used in
|
| 72 |
+
the `sizeof` operator. — *end note*]
|
| 73 |
|
| 74 |
Two expressions involving template parameters are considered
|
| 75 |
*equivalent* if two function definitions containing the expressions
|
| 76 |
+
would satisfy the one-definition rule ([[basic.def.odr]]), except that
|
| 77 |
the tokens used to name the template parameters may differ as long as a
|
| 78 |
token used to name a template parameter in one expression is replaced by
|
| 79 |
another token that names the same template parameter in the other
|
| 80 |
expression. For determining whether two dependent names ([[temp.dep]])
|
| 81 |
are equivalent, only the name itself is considered, not the result of
|
| 82 |
name lookup in the context of the template. If multiple declarations of
|
| 83 |
the same function template differ in the result of this name lookup, the
|
| 84 |
result for the first declaration is used.
|
| 85 |
|
| 86 |
+
[*Example 3*:
|
| 87 |
+
|
| 88 |
``` cpp
|
| 89 |
template <int I, int J> void f(A<I+J>); // #1
|
| 90 |
template <int K, int L> void f(A<K+L>); // same as #1
|
| 91 |
|
| 92 |
template <class T> decltype(g(T())) h();
|
|
|
|
| 95 |
{ return g(T()); } // ...although the lookup here does find g(int)
|
| 96 |
int i = h<int>(); // template argument substitution fails; g(int)
|
| 97 |
// was not in scope at the first declaration of h()
|
| 98 |
```
|
| 99 |
|
| 100 |
+
— *end example*]
|
| 101 |
+
|
| 102 |
Two expressions involving template parameters that are not equivalent
|
| 103 |
are *functionally equivalent* if, for any given set of template
|
| 104 |
arguments, the evaluation of the expression results in the same value.
|
| 105 |
|
| 106 |
Two function templates are *equivalent* if they are declared in the same
|
|
|
|
| 111 |
are equivalent except that one or more expressions that involve template
|
| 112 |
parameters in the return types and parameter lists are functionally
|
| 113 |
equivalent using the rules described above to compare expressions
|
| 114 |
involving template parameters. If a program contains declarations of
|
| 115 |
function templates that are functionally equivalent but not equivalent,
|
| 116 |
+
the program is ill-formed, no diagnostic required.
|
| 117 |
+
|
| 118 |
+
[*Note 3*:
|
| 119 |
|
| 120 |
This rule guarantees that equivalent declarations will be linked with
|
| 121 |
one another, while not requiring implementations to use heroic efforts
|
| 122 |
to guarantee that functionally equivalent declarations will be treated
|
| 123 |
as distinct. For example, the last two declarations are functionally
|
|
|
|
| 135 |
// Ill-formed, no diagnostic required
|
| 136 |
template <int I> void f(A<I>, A<I+10>);
|
| 137 |
template <int I> void f(A<I>, A<I+1+2+3+4>);
|
| 138 |
```
|
| 139 |
|
| 140 |
+
— *end note*]
|
| 141 |
+
|