tmp/tmp6trc_dfi/{from.md → to.md}
RENAMED
|
@@ -3,10 +3,12 @@
|
|
| 3 |
Template arguments can be specified when referring to a function
|
| 4 |
template specialization by qualifying the function template name with
|
| 5 |
the list of *template-argument*s in the same way as *template-argument*s
|
| 6 |
are specified in uses of a class template specialization.
|
| 7 |
|
|
|
|
|
|
|
| 8 |
``` cpp
|
| 9 |
template<class T> void sort(Array<T>& v);
|
| 10 |
void f(Array<dcomplex>& cv, Array<int>& ci) {
|
| 11 |
sort<dcomplex>(cv); // sort(Array<dcomplex>&)
|
| 12 |
sort<int>(ci); // sort(Array<int>&)
|
|
@@ -22,10 +24,12 @@ void g(double d) {
|
|
| 22 |
int i = convert<int,double>(d); // int convert(double)
|
| 23 |
char c = convert<char,double>(d); // char convert(double)
|
| 24 |
}
|
| 25 |
```
|
| 26 |
|
|
|
|
|
|
|
| 27 |
A template argument list may be specified when referring to a
|
| 28 |
specialization of a function template
|
| 29 |
|
| 30 |
- when a function is called,
|
| 31 |
- when the address of a function is taken, when a function initializes a
|
|
@@ -45,26 +49,30 @@ deduction is done and fails, or in contexts where deduction is not done,
|
|
| 45 |
if a template argument list is specified and it, along with any default
|
| 46 |
template arguments, identifies a single function template
|
| 47 |
specialization, then the *template-id* is an lvalue for the function
|
| 48 |
template specialization.
|
| 49 |
|
|
|
|
|
|
|
| 50 |
``` cpp
|
| 51 |
template<class X, class Y> X f(Y);
|
| 52 |
template<class X, class Y, class ... Z> X g(Y);
|
| 53 |
void h() {
|
| 54 |
int i = f<int>(5.6); // Y is deduced to be double
|
| 55 |
int j = f(5.6); // ill-formed: X cannot be deduced
|
| 56 |
-
f<void>(f<int, bool>); // Y for outer f deduced to be
|
| 57 |
-
|
| 58 |
-
f<void>(f<int>); // ill-formed: f<int> does not denote a
|
| 59 |
-
// single function template specialization
|
| 60 |
int k = g<int>(5.6); // Y is deduced to be double, Z is deduced to an empty sequence
|
| 61 |
-
f<void>(g<int, bool>); // Y for outer f is deduced to be
|
| 62 |
-
//
|
| 63 |
}
|
| 64 |
```
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
An empty template argument list can be used to indicate that a given use
|
| 67 |
refers to a specialization of a function template even when a
|
| 68 |
non-template function ([[dcl.fct]]) is visible that would otherwise be
|
| 69 |
used. For example:
|
| 70 |
|
|
@@ -73,35 +81,43 @@ template <class T> int f(T); // #1
|
|
| 73 |
int f(int); // #2
|
| 74 |
int k = f(1); // uses #2
|
| 75 |
int l = f<>(1); // uses #1
|
| 76 |
```
|
| 77 |
|
|
|
|
|
|
|
| 78 |
Template arguments that are present shall be specified in the
|
| 79 |
declaration order of their corresponding *template-parameter*s. The
|
| 80 |
template argument list shall not specify more *template-argument*s than
|
| 81 |
there are corresponding *template-parameter*s unless one of the
|
| 82 |
*template-parameter*s is a template parameter pack.
|
| 83 |
|
|
|
|
|
|
|
| 84 |
``` cpp
|
| 85 |
template<class X, class Y, class Z> X f(Y,Z);
|
| 86 |
template<class ... Args> void f2();
|
| 87 |
void g() {
|
| 88 |
f<int,const char*,double>("aa",3.0);
|
| 89 |
f<int,const char*>("aa",3.0); // Z is deduced to be double
|
| 90 |
-
f<int>("aa",3.0); // Y is deduced to be const char*, and
|
| 91 |
-
// Z is deduced to be double
|
| 92 |
f("aa",3.0); // error: X cannot be deduced
|
| 93 |
f2<char, short, int, long>(); // OK
|
| 94 |
}
|
| 95 |
```
|
| 96 |
|
|
|
|
|
|
|
| 97 |
Implicit conversions (Clause [[conv]]) will be performed on a function
|
| 98 |
argument to convert it to the type of the corresponding function
|
| 99 |
parameter if the parameter type contains no *template-parameter*s that
|
| 100 |
-
participate in template argument deduction.
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
``` cpp
|
| 105 |
template<class T> void f(T);
|
| 106 |
|
| 107 |
class Complex {
|
|
@@ -111,15 +127,19 @@ class Complex {
|
|
| 111 |
void g() {
|
| 112 |
f<Complex>(1); // OK, means f<Complex>(Complex(1))
|
| 113 |
}
|
| 114 |
```
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
| 119 |
function name, there is no way to provide an explicit template argument
|
| 120 |
-
list for these function templates.
|
|
|
|
|
|
|
| 121 |
|
| 122 |
For simple function names, argument dependent lookup (
|
| 123 |
[[basic.lookup.argdep]]) applies even when the function name is not
|
| 124 |
visible within the scope of the call. This is because the call still has
|
| 125 |
the syntactic form of a function call ([[basic.lookup.unqual]]). But
|
|
@@ -129,10 +149,12 @@ template with that name visible at the point of the call. If no such
|
|
| 129 |
name is visible, the call is not syntactically well-formed and
|
| 130 |
argument-dependent lookup does not apply. If some such name is visible,
|
| 131 |
argument dependent lookup applies and additional function templates may
|
| 132 |
be found in other namespaces.
|
| 133 |
|
|
|
|
|
|
|
| 134 |
``` cpp
|
| 135 |
namespace A {
|
| 136 |
struct B { };
|
| 137 |
template<int X> void f(B);
|
| 138 |
}
|
|
@@ -140,25 +162,31 @@ namespace C {
|
|
| 140 |
template<class T> void f(T t);
|
| 141 |
}
|
| 142 |
void g(A::B b) {
|
| 143 |
f<3>(b); // ill-formed: not a function call
|
| 144 |
A::f<3>(b); // well-formed
|
| 145 |
-
C::f<3>(b);
|
| 146 |
-
// applies only to unqualified names
|
| 147 |
using C::f;
|
| 148 |
-
f<3>(b);
|
| 149 |
-
// A::f is found by argument dependent lookup
|
| 150 |
}
|
| 151 |
```
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
Template argument deduction can extend the sequence of template
|
| 154 |
arguments corresponding to a template parameter pack, even when the
|
| 155 |
sequence contains explicitly specified template arguments.
|
| 156 |
|
|
|
|
|
|
|
| 157 |
``` cpp
|
| 158 |
template<class ... Types> void f(Types ... values);
|
| 159 |
|
| 160 |
void g() {
|
| 161 |
f<int*, float*>(0, 0, 0); // Types is deduced to the sequence int*, float*, int
|
| 162 |
}
|
| 163 |
```
|
| 164 |
|
|
|
|
|
|
|
|
|
| 3 |
Template arguments can be specified when referring to a function
|
| 4 |
template specialization by qualifying the function template name with
|
| 5 |
the list of *template-argument*s in the same way as *template-argument*s
|
| 6 |
are specified in uses of a class template specialization.
|
| 7 |
|
| 8 |
+
[*Example 1*:
|
| 9 |
+
|
| 10 |
``` cpp
|
| 11 |
template<class T> void sort(Array<T>& v);
|
| 12 |
void f(Array<dcomplex>& cv, Array<int>& ci) {
|
| 13 |
sort<dcomplex>(cv); // sort(Array<dcomplex>&)
|
| 14 |
sort<int>(ci); // sort(Array<int>&)
|
|
|
|
| 24 |
int i = convert<int,double>(d); // int convert(double)
|
| 25 |
char c = convert<char,double>(d); // char convert(double)
|
| 26 |
}
|
| 27 |
```
|
| 28 |
|
| 29 |
+
— *end example*]
|
| 30 |
+
|
| 31 |
A template argument list may be specified when referring to a
|
| 32 |
specialization of a function template
|
| 33 |
|
| 34 |
- when a function is called,
|
| 35 |
- when the address of a function is taken, when a function initializes a
|
|
|
|
| 49 |
if a template argument list is specified and it, along with any default
|
| 50 |
template arguments, identifies a single function template
|
| 51 |
specialization, then the *template-id* is an lvalue for the function
|
| 52 |
template specialization.
|
| 53 |
|
| 54 |
+
[*Example 2*:
|
| 55 |
+
|
| 56 |
``` cpp
|
| 57 |
template<class X, class Y> X f(Y);
|
| 58 |
template<class X, class Y, class ... Z> X g(Y);
|
| 59 |
void h() {
|
| 60 |
int i = f<int>(5.6); // Y is deduced to be double
|
| 61 |
int j = f(5.6); // ill-formed: X cannot be deduced
|
| 62 |
+
f<void>(f<int, bool>); // Y for outer f deduced to be int (*)(bool)
|
| 63 |
+
f<void>(f<int>); // ill-formed: f<int> does not denote a single function template specialization
|
|
|
|
|
|
|
| 64 |
int k = g<int>(5.6); // Y is deduced to be double, Z is deduced to an empty sequence
|
| 65 |
+
f<void>(g<int, bool>); // Y for outer f is deduced to be int (*)(bool),
|
| 66 |
+
// Z is deduced to an empty sequence
|
| 67 |
}
|
| 68 |
```
|
| 69 |
|
| 70 |
+
— *end example*]
|
| 71 |
+
|
| 72 |
+
[*Note 1*:
|
| 73 |
+
|
| 74 |
An empty template argument list can be used to indicate that a given use
|
| 75 |
refers to a specialization of a function template even when a
|
| 76 |
non-template function ([[dcl.fct]]) is visible that would otherwise be
|
| 77 |
used. For example:
|
| 78 |
|
|
|
|
| 81 |
int f(int); // #2
|
| 82 |
int k = f(1); // uses #2
|
| 83 |
int l = f<>(1); // uses #1
|
| 84 |
```
|
| 85 |
|
| 86 |
+
— *end note*]
|
| 87 |
+
|
| 88 |
Template arguments that are present shall be specified in the
|
| 89 |
declaration order of their corresponding *template-parameter*s. The
|
| 90 |
template argument list shall not specify more *template-argument*s than
|
| 91 |
there are corresponding *template-parameter*s unless one of the
|
| 92 |
*template-parameter*s is a template parameter pack.
|
| 93 |
|
| 94 |
+
[*Example 3*:
|
| 95 |
+
|
| 96 |
``` cpp
|
| 97 |
template<class X, class Y, class Z> X f(Y,Z);
|
| 98 |
template<class ... Args> void f2();
|
| 99 |
void g() {
|
| 100 |
f<int,const char*,double>("aa",3.0);
|
| 101 |
f<int,const char*>("aa",3.0); // Z is deduced to be double
|
| 102 |
+
f<int>("aa",3.0); // Y is deduced to be const char*, and Z is deduced to be double
|
|
|
|
| 103 |
f("aa",3.0); // error: X cannot be deduced
|
| 104 |
f2<char, short, int, long>(); // OK
|
| 105 |
}
|
| 106 |
```
|
| 107 |
|
| 108 |
+
— *end example*]
|
| 109 |
+
|
| 110 |
Implicit conversions (Clause [[conv]]) will be performed on a function
|
| 111 |
argument to convert it to the type of the corresponding function
|
| 112 |
parameter if the parameter type contains no *template-parameter*s that
|
| 113 |
+
participate in template argument deduction.
|
| 114 |
+
|
| 115 |
+
[*Note 2*:
|
| 116 |
+
|
| 117 |
+
Template parameters do not participate in template argument deduction if
|
| 118 |
+
they are explicitly specified. For example,
|
| 119 |
|
| 120 |
``` cpp
|
| 121 |
template<class T> void f(T);
|
| 122 |
|
| 123 |
class Complex {
|
|
|
|
| 127 |
void g() {
|
| 128 |
f<Complex>(1); // OK, means f<Complex>(Complex(1))
|
| 129 |
}
|
| 130 |
```
|
| 131 |
|
| 132 |
+
— *end note*]
|
| 133 |
+
|
| 134 |
+
[*Note 3*: Because the explicit template argument list follows the
|
| 135 |
+
function template name, and because conversion member function templates
|
| 136 |
+
and constructor member function templates are called without using a
|
| 137 |
function name, there is no way to provide an explicit template argument
|
| 138 |
+
list for these function templates. — *end note*]
|
| 139 |
+
|
| 140 |
+
[*Note 4*:
|
| 141 |
|
| 142 |
For simple function names, argument dependent lookup (
|
| 143 |
[[basic.lookup.argdep]]) applies even when the function name is not
|
| 144 |
visible within the scope of the call. This is because the call still has
|
| 145 |
the syntactic form of a function call ([[basic.lookup.unqual]]). But
|
|
|
|
| 149 |
name is visible, the call is not syntactically well-formed and
|
| 150 |
argument-dependent lookup does not apply. If some such name is visible,
|
| 151 |
argument dependent lookup applies and additional function templates may
|
| 152 |
be found in other namespaces.
|
| 153 |
|
| 154 |
+
[*Example 4*:
|
| 155 |
+
|
| 156 |
``` cpp
|
| 157 |
namespace A {
|
| 158 |
struct B { };
|
| 159 |
template<int X> void f(B);
|
| 160 |
}
|
|
|
|
| 162 |
template<class T> void f(T t);
|
| 163 |
}
|
| 164 |
void g(A::B b) {
|
| 165 |
f<3>(b); // ill-formed: not a function call
|
| 166 |
A::f<3>(b); // well-formed
|
| 167 |
+
C::f<3>(b); // ill-formed; argument dependent lookup applies only to unqualified names
|
|
|
|
| 168 |
using C::f;
|
| 169 |
+
f<3>(b); // well-formed because C::f is visible; then A::f is found by argument dependent lookup
|
|
|
|
| 170 |
}
|
| 171 |
```
|
| 172 |
|
| 173 |
+
— *end example*]
|
| 174 |
+
|
| 175 |
+
— *end note*]
|
| 176 |
+
|
| 177 |
Template argument deduction can extend the sequence of template
|
| 178 |
arguments corresponding to a template parameter pack, even when the
|
| 179 |
sequence contains explicitly specified template arguments.
|
| 180 |
|
| 181 |
+
[*Example 5*:
|
| 182 |
+
|
| 183 |
``` cpp
|
| 184 |
template<class ... Types> void f(Types ... values);
|
| 185 |
|
| 186 |
void g() {
|
| 187 |
f<int*, float*>(0, 0, 0); // Types is deduced to the sequence int*, float*, int
|
| 188 |
}
|
| 189 |
```
|
| 190 |
|
| 191 |
+
— *end example*]
|
| 192 |
+
|