tmp/tmp080cdedz/{from.md → to.md}
RENAMED
|
@@ -3,84 +3,93 @@
|
|
| 3 |
Template argument deduction is done by comparing each function template
|
| 4 |
parameter type (call it `P`) that contains *template-parameter*s that
|
| 5 |
participate in template argument deduction with the type of the
|
| 6 |
corresponding argument of the call (call it `A`) as described below. If
|
| 7 |
removing references and cv-qualifiers from `P` gives
|
| 8 |
-
`std::initializer_list<P
|
| 9 |
-
argument is a non-empty initializer list
|
| 10 |
-
deduction is performed instead for each element of the initializer list
|
| 11 |
-
taking `P
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
[*Example 1*:
|
| 18 |
|
| 19 |
``` cpp
|
| 20 |
template<class T> void f(std::initializer_list<T>);
|
| 21 |
-
f({1,2,3}); // T deduced
|
| 22 |
-
f({1,"asdf"}); // error: T deduced
|
| 23 |
|
| 24 |
template<class T> void g(T);
|
| 25 |
g({1,2,3}); // error: no argument deduced for T
|
| 26 |
|
| 27 |
template<class T, int N> void h(T const(&)[N]);
|
| 28 |
-
h({1,2,3}); // T deduced
|
| 29 |
|
| 30 |
template<class T> void j(T const(&)[3]);
|
| 31 |
-
j({42}); // T deduced
|
| 32 |
|
| 33 |
struct Aggr { int i; int j; };
|
| 34 |
template<int N> void k(Aggr const(&)[N]);
|
| 35 |
k({1,2,3}); // error: deduction fails, no conversion from int to Aggr
|
| 36 |
-
k({{1},{2},{3}}); // OK, N deduced
|
| 37 |
|
| 38 |
template<int M, int N> void m(int const(&)[M][N]);
|
| 39 |
-
m({{1,2},{3,4}}); // M and N both deduced
|
| 40 |
|
| 41 |
template<class T, int N> void n(T const(&)[N], T);
|
| 42 |
n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
```
|
| 44 |
|
| 45 |
— *end example*]
|
| 46 |
|
| 47 |
For a function parameter pack that occurs at the end of the
|
| 48 |
*parameter-declaration-list*, deduction is performed for each remaining
|
| 49 |
argument of the call, taking the type `P` of the *declarator-id* of the
|
| 50 |
function parameter pack as the corresponding function template parameter
|
| 51 |
type. Each deduction deduces template arguments for subsequent positions
|
| 52 |
in the template parameter packs expanded by the function parameter pack.
|
| 53 |
-
When a function parameter pack appears in a non-deduced context
|
| 54 |
-
[[temp.deduct.type]]
|
| 55 |
|
| 56 |
[*Example 2*:
|
| 57 |
|
| 58 |
``` cpp
|
| 59 |
template<class ... Types> void f(Types& ...);
|
| 60 |
template<class T1, class ... Types> void g(T1, Types ...);
|
| 61 |
template<class T1, class ... Types> void g1(Types ..., T1);
|
| 62 |
|
| 63 |
void h(int x, float& y) {
|
| 64 |
const int z = x;
|
| 65 |
-
f(x, y, z); // Types
|
| 66 |
-
g(x, y, z); // T1
|
| 67 |
g1(x, y, z); // error: Types is not deduced
|
| 68 |
g1<int, int, int>(x, y, z); // OK, no deduction occurs
|
| 69 |
}
|
| 70 |
```
|
| 71 |
|
| 72 |
— *end example*]
|
| 73 |
|
| 74 |
If `P` is not a reference type:
|
| 75 |
|
| 76 |
- If `A` is an array type, the pointer type produced by the
|
| 77 |
-
array-to-pointer standard conversion
|
| 78 |
-
|
| 79 |
- If `A` is a function type, the pointer type produced by the
|
| 80 |
-
function-to-pointer standard conversion
|
| 81 |
-
|
| 82 |
- If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s
|
| 83 |
type are ignored for type deduction.
|
| 84 |
|
| 85 |
If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s type
|
| 86 |
are ignored for type deduction. If `P` is a reference type, the type
|
|
@@ -99,12 +108,12 @@ int n3 = g(i); // calls g<const int>(const volatile int&)
|
|
| 99 |
|
| 100 |
— *end example*]
|
| 101 |
|
| 102 |
A *forwarding reference* is an rvalue reference to a cv-unqualified
|
| 103 |
template parameter that does not represent a template parameter of a
|
| 104 |
-
class template (during class template argument deduction
|
| 105 |
-
[[over.match.class.deduct]])
|
| 106 |
argument is an lvalue, the type “lvalue reference to `A`” is used in
|
| 107 |
place of `A` for type deduction.
|
| 108 |
|
| 109 |
[*Example 4*:
|
| 110 |
|
|
@@ -140,19 +149,38 @@ values that will make the deduced `A` identical to `A` (after the type
|
|
| 140 |
that allow a difference:
|
| 141 |
|
| 142 |
- If the original `P` is a reference type, the deduced `A` (i.e., the
|
| 143 |
type referred to by the reference) can be more cv-qualified than the
|
| 144 |
transformed `A`.
|
| 145 |
-
- The transformed `A` can be another pointer or pointer
|
| 146 |
that can be converted to the deduced `A` via a function pointer
|
| 147 |
-
conversion
|
| 148 |
-
[[conv.qual]]
|
| 149 |
- If `P` is a class and `P` has the form *simple-template-id*, then the
|
| 150 |
-
transformed `A` can be a derived class of the deduced `A`.
|
| 151 |
-
if `P` is a pointer to a class of the form
|
| 152 |
-
transformed `A` can be a pointer to a
|
| 153 |
-
deduced `A`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
These alternatives are considered only if type deduction would otherwise
|
| 156 |
fail. If they yield more than one possible deduced `A`, the type
|
| 157 |
deduction fails.
|
| 158 |
|
|
@@ -160,23 +188,23 @@ deduction fails.
|
|
| 160 |
parameters of a function template, or is used only in a non-deduced
|
| 161 |
context, its corresponding *template-argument* cannot be deduced from a
|
| 162 |
function call and the *template-argument* must be explicitly
|
| 163 |
specified. — *end note*]
|
| 164 |
|
| 165 |
-
When `P` is a function type, function pointer type, or
|
| 166 |
-
function type:
|
| 167 |
|
| 168 |
- If the argument is an overload set containing one or more function
|
| 169 |
templates, the parameter is treated as a non-deduced context.
|
| 170 |
- If the argument is an overload set (not containing function
|
| 171 |
templates), trial argument deduction is attempted using each of the
|
| 172 |
members of the set. If deduction succeeds for only one of the overload
|
| 173 |
set members, that member is used as the argument value for the
|
| 174 |
deduction. If deduction succeeds for more than one member of the
|
| 175 |
overload set the parameter is treated as a non-deduced context.
|
| 176 |
|
| 177 |
-
[*Example
|
| 178 |
|
| 179 |
``` cpp
|
| 180 |
// Only one function of an overload set matches the call so the function parameter is a deduced context.
|
| 181 |
template <class T> int f(T (*p)(T));
|
| 182 |
int g(int);
|
|
@@ -184,24 +212,24 @@ int g(char);
|
|
| 184 |
int i = f(g); // calls f(int (*)(int))
|
| 185 |
```
|
| 186 |
|
| 187 |
— *end example*]
|
| 188 |
|
| 189 |
-
[*Example 6*:
|
| 190 |
-
|
| 191 |
-
``` cpp
|
| 192 |
-
// Ambiguous deduction causes the second function parameter to be a non-deduced context.
|
| 193 |
-
template <class T> int f(T, T (*p)(T));
|
| 194 |
-
int g(int);
|
| 195 |
-
char g(char);
|
| 196 |
-
int i = f(1, g); // calls f(int, int (*)(int))
|
| 197 |
-
```
|
| 198 |
-
|
| 199 |
-
— *end example*]
|
| 200 |
-
|
| 201 |
[*Example 7*:
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
``` cpp
|
| 204 |
// The overload set contains a template, causing the second function parameter to be a non-deduced context.
|
| 205 |
template <class T> int f(T, T (*p)(T));
|
| 206 |
char g(char);
|
| 207 |
template <class T> T g(T);
|
|
@@ -223,11 +251,11 @@ explicitly-specified template arguments, if the corresponding argument
|
|
| 223 |
*template-parameter*s participate in template argument deduction, and
|
| 224 |
parameters that became non-dependent due to substitution of
|
| 225 |
explicitly-specified template arguments, will be checked during overload
|
| 226 |
resolution. — *end note*]
|
| 227 |
|
| 228 |
-
[*Example
|
| 229 |
|
| 230 |
``` cpp
|
| 231 |
template <class T> struct Z {
|
| 232 |
typedef typename T::x xx;
|
| 233 |
};
|
|
|
|
| 3 |
Template argument deduction is done by comparing each function template
|
| 4 |
parameter type (call it `P`) that contains *template-parameter*s that
|
| 5 |
participate in template argument deduction with the type of the
|
| 6 |
corresponding argument of the call (call it `A`) as described below. If
|
| 7 |
removing references and cv-qualifiers from `P` gives
|
| 8 |
+
`std::initializer_list<P^{\prime}>` or `P`'`[N]` for some `P`' and `N`
|
| 9 |
+
and the argument is a non-empty initializer list [[dcl.init.list]], then
|
| 10 |
+
deduction is performed instead for each element of the initializer list
|
| 11 |
+
independently, taking `P`' as separate function template parameter types
|
| 12 |
+
`P`'_i and the iᵗʰ initializer element as the corresponding argument. In
|
| 13 |
+
the `P`'`[N]` case, if `N` is a non-type template parameter, `N` is
|
| 14 |
+
deduced from the length of the initializer list. Otherwise, an
|
| 15 |
+
initializer list argument causes the parameter to be considered a
|
| 16 |
+
non-deduced context [[temp.deduct.type]].
|
| 17 |
|
| 18 |
[*Example 1*:
|
| 19 |
|
| 20 |
``` cpp
|
| 21 |
template<class T> void f(std::initializer_list<T>);
|
| 22 |
+
f({1,2,3}); // T deduced as int
|
| 23 |
+
f({1,"asdf"}); // error: T deduced as both int and const char*
|
| 24 |
|
| 25 |
template<class T> void g(T);
|
| 26 |
g({1,2,3}); // error: no argument deduced for T
|
| 27 |
|
| 28 |
template<class T, int N> void h(T const(&)[N]);
|
| 29 |
+
h({1,2,3}); // T deduced as int; N deduced as 3
|
| 30 |
|
| 31 |
template<class T> void j(T const(&)[3]);
|
| 32 |
+
j({42}); // T deduced as int; array bound not considered
|
| 33 |
|
| 34 |
struct Aggr { int i; int j; };
|
| 35 |
template<int N> void k(Aggr const(&)[N]);
|
| 36 |
k({1,2,3}); // error: deduction fails, no conversion from int to Aggr
|
| 37 |
+
k({{1},{2},{3}}); // OK, N deduced as 3
|
| 38 |
|
| 39 |
template<int M, int N> void m(int const(&)[M][N]);
|
| 40 |
+
m({{1,2},{3,4}}); // M and N both deduced as 2
|
| 41 |
|
| 42 |
template<class T, int N> void n(T const(&)[N], T);
|
| 43 |
n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
|
| 44 |
+
|
| 45 |
+
template<typename T, int N> void o(T (* const (&)[N])(T)) { }
|
| 46 |
+
int f1(int);
|
| 47 |
+
int f4(int);
|
| 48 |
+
char f4(char);
|
| 49 |
+
o({ &f1, &f4 }); // OK, T deduced as int from first element, nothing
|
| 50 |
+
// deduced from second element, N deduced as 2
|
| 51 |
+
o({ &f1, static_cast<char(*)(char)>(&f4) }); // error: conflicting deductions for T
|
| 52 |
```
|
| 53 |
|
| 54 |
— *end example*]
|
| 55 |
|
| 56 |
For a function parameter pack that occurs at the end of the
|
| 57 |
*parameter-declaration-list*, deduction is performed for each remaining
|
| 58 |
argument of the call, taking the type `P` of the *declarator-id* of the
|
| 59 |
function parameter pack as the corresponding function template parameter
|
| 60 |
type. Each deduction deduces template arguments for subsequent positions
|
| 61 |
in the template parameter packs expanded by the function parameter pack.
|
| 62 |
+
When a function parameter pack appears in a non-deduced context
|
| 63 |
+
[[temp.deduct.type]], the type of that pack is never deduced.
|
| 64 |
|
| 65 |
[*Example 2*:
|
| 66 |
|
| 67 |
``` cpp
|
| 68 |
template<class ... Types> void f(Types& ...);
|
| 69 |
template<class T1, class ... Types> void g(T1, Types ...);
|
| 70 |
template<class T1, class ... Types> void g1(Types ..., T1);
|
| 71 |
|
| 72 |
void h(int x, float& y) {
|
| 73 |
const int z = x;
|
| 74 |
+
f(x, y, z); // Types deduced as int, float, const int
|
| 75 |
+
g(x, y, z); // T1 deduced as int; Types deduced as float, int
|
| 76 |
g1(x, y, z); // error: Types is not deduced
|
| 77 |
g1<int, int, int>(x, y, z); // OK, no deduction occurs
|
| 78 |
}
|
| 79 |
```
|
| 80 |
|
| 81 |
— *end example*]
|
| 82 |
|
| 83 |
If `P` is not a reference type:
|
| 84 |
|
| 85 |
- If `A` is an array type, the pointer type produced by the
|
| 86 |
+
array-to-pointer standard conversion [[conv.array]] is used in place
|
| 87 |
+
of `A` for type deduction; otherwise,
|
| 88 |
- If `A` is a function type, the pointer type produced by the
|
| 89 |
+
function-to-pointer standard conversion [[conv.func]] is used in place
|
| 90 |
+
of `A` for type deduction; otherwise,
|
| 91 |
- If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s
|
| 92 |
type are ignored for type deduction.
|
| 93 |
|
| 94 |
If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s type
|
| 95 |
are ignored for type deduction. If `P` is a reference type, the type
|
|
|
|
| 108 |
|
| 109 |
— *end example*]
|
| 110 |
|
| 111 |
A *forwarding reference* is an rvalue reference to a cv-unqualified
|
| 112 |
template parameter that does not represent a template parameter of a
|
| 113 |
+
class template (during class template argument deduction
|
| 114 |
+
[[over.match.class.deduct]]). If `P` is a forwarding reference and the
|
| 115 |
argument is an lvalue, the type “lvalue reference to `A`” is used in
|
| 116 |
place of `A` for type deduction.
|
| 117 |
|
| 118 |
[*Example 4*:
|
| 119 |
|
|
|
|
| 149 |
that allow a difference:
|
| 150 |
|
| 151 |
- If the original `P` is a reference type, the deduced `A` (i.e., the
|
| 152 |
type referred to by the reference) can be more cv-qualified than the
|
| 153 |
transformed `A`.
|
| 154 |
+
- The transformed `A` can be another pointer or pointer-to-member type
|
| 155 |
that can be converted to the deduced `A` via a function pointer
|
| 156 |
+
conversion [[conv.fctptr]] and/or qualification conversion
|
| 157 |
+
[[conv.qual]].
|
| 158 |
- If `P` is a class and `P` has the form *simple-template-id*, then the
|
| 159 |
+
transformed `A` can be a derived class `D` of the deduced `A`.
|
| 160 |
+
Likewise, if `P` is a pointer to a class of the form
|
| 161 |
+
*simple-template-id*, the transformed `A` can be a pointer to a
|
| 162 |
+
derived class `D` pointed to by the deduced `A`. However, if there is
|
| 163 |
+
a class `C` that is a (direct or indirect) base class of `D` and
|
| 164 |
+
derived (directly or indirectly) from a class `B` and that would be a
|
| 165 |
+
valid deduced `A`, the deduced `A` cannot be `B` or pointer to `B`,
|
| 166 |
+
respectively.
|
| 167 |
+
\[*Example 5*:
|
| 168 |
+
``` cpp
|
| 169 |
+
template <typename... T> struct X;
|
| 170 |
+
template <> struct X<> {};
|
| 171 |
+
template <typename T, typename... Ts>
|
| 172 |
+
struct X<T, Ts...> : X<Ts...> {};
|
| 173 |
+
struct D : X<int> {};
|
| 174 |
+
|
| 175 |
+
template <typename... T>
|
| 176 |
+
int f(const X<T...>&);
|
| 177 |
+
int x = f(D()); // calls f<int>, not f<>
|
| 178 |
+
// B is X<>, C is X<int>
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
— *end example*]
|
| 182 |
|
| 183 |
These alternatives are considered only if type deduction would otherwise
|
| 184 |
fail. If they yield more than one possible deduced `A`, the type
|
| 185 |
deduction fails.
|
| 186 |
|
|
|
|
| 188 |
parameters of a function template, or is used only in a non-deduced
|
| 189 |
context, its corresponding *template-argument* cannot be deduced from a
|
| 190 |
function call and the *template-argument* must be explicitly
|
| 191 |
specified. — *end note*]
|
| 192 |
|
| 193 |
+
When `P` is a function type, function pointer type, or
|
| 194 |
+
pointer-to-member-function type:
|
| 195 |
|
| 196 |
- If the argument is an overload set containing one or more function
|
| 197 |
templates, the parameter is treated as a non-deduced context.
|
| 198 |
- If the argument is an overload set (not containing function
|
| 199 |
templates), trial argument deduction is attempted using each of the
|
| 200 |
members of the set. If deduction succeeds for only one of the overload
|
| 201 |
set members, that member is used as the argument value for the
|
| 202 |
deduction. If deduction succeeds for more than one member of the
|
| 203 |
overload set the parameter is treated as a non-deduced context.
|
| 204 |
|
| 205 |
+
[*Example 6*:
|
| 206 |
|
| 207 |
``` cpp
|
| 208 |
// Only one function of an overload set matches the call so the function parameter is a deduced context.
|
| 209 |
template <class T> int f(T (*p)(T));
|
| 210 |
int g(int);
|
|
|
|
| 212 |
int i = f(g); // calls f(int (*)(int))
|
| 213 |
```
|
| 214 |
|
| 215 |
— *end example*]
|
| 216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
[*Example 7*:
|
| 218 |
|
| 219 |
+
``` cpp
|
| 220 |
+
// Ambiguous deduction causes the second function parameter to be a non-deduced context.
|
| 221 |
+
template <class T> int f(T, T (*p)(T));
|
| 222 |
+
int g(int);
|
| 223 |
+
char g(char);
|
| 224 |
+
int i = f(1, g); // calls f(int, int (*)(int))
|
| 225 |
+
```
|
| 226 |
+
|
| 227 |
+
— *end example*]
|
| 228 |
+
|
| 229 |
+
[*Example 8*:
|
| 230 |
+
|
| 231 |
``` cpp
|
| 232 |
// The overload set contains a template, causing the second function parameter to be a non-deduced context.
|
| 233 |
template <class T> int f(T, T (*p)(T));
|
| 234 |
char g(char);
|
| 235 |
template <class T> T g(T);
|
|
|
|
| 251 |
*template-parameter*s participate in template argument deduction, and
|
| 252 |
parameters that became non-dependent due to substitution of
|
| 253 |
explicitly-specified template arguments, will be checked during overload
|
| 254 |
resolution. — *end note*]
|
| 255 |
|
| 256 |
+
[*Example 9*:
|
| 257 |
|
| 258 |
``` cpp
|
| 259 |
template <class T> struct Z {
|
| 260 |
typedef typename T::x xx;
|
| 261 |
};
|