c++ - What are the rules for choosing from overloaded template functions? -
Looking at the code given below, why the foo (T *)
function is selected?
If I delete it (code foo (T *)
) still works and works correctly, but G ++ v4.4.0 (and maybe other Compiler too) Two Fu ()
functions: four for one [four] and four for one [7]
#include & lt; Iostream & gt; using namespace std; The template's & lt; Tinnamam T & GT; Void foo (CONST T & amp; Cout & lt; & Lt; "Foo (Constant & amp; amp)" & lt; & Lt; Endl; } Template & lt; Tinnamam T & GT; Zero Fu (T *) {cout & lt; & Lt; "Foo (t *)" & lt; & Lt; Endl; } Int main () {foo ("bar"); Fu ("Furar"); Return 0; Formally, when comparing conversion sequences, lvalue changes are ignored. The Eligibility Adjustment (< Code> T * -> T const *
), changes to the ( int [n]
-> int *
, zero ()
-> zero (*) ()
), and the other. The only difference between your two candidates is a renormal change. String literals are arrays that change the indicator. The first candidate accepts the array from the context, and thus no changes will be needed. The second candidate needs to change the value.
Therefore, if there are two candidates that both the function template experts are equally viable by looking at conversions only, then the rule is that by the fractional order of two characteristics more The specific person is chosen.
Let's see the signature of their function parameter list
zero (T const & amp;;); Zero (T *);
If we select some specific type Q
for the first parameter list and try to match the second parameter list, then we will see the Q < Matching with / code> Contrary to T *
. This will be unsuccessful, because Q
is not an indicator, so the second is at least as specific as before
If we do the other way, then we will get the Against Q *
against T const & amp;
. The reference has been dropped and the upper qualifiers are ignored, and the remaining T
becomes Q *
This is an exact match for the purpose of partial order, and this Type deduction of changed list of second against first candidate is successful as second direction (against the other) has not succeeded, other candidate is special more - and as a result, overload resolution Second one
Standard conversion sequence S1 standard conversion sequence better than S2 If the conversion sequence is [...]
- S1 (S1) is a proper poster of S2 (13.3.3.1.1 as defined by the comparison of conversion sequences in an atomic form, Apart from the lavaluoo changes, the identity conversion sequence is considered to be followed by any non-identifying conversion sequence. ) Or, if not [...]
then 13.3.3 / 1
- ICSI (F) denotes the underlying conversion sequence which lists the i-th argument in the functional function F. Converts I-th parameter types into 13.3.3.1, the built-in conversion sequence and 13.3.3.2 define that in the form of a better conversion sequence or a converted conversion sequence than any other conversion, this means an inherent conversion sequence for.
In view of these definitions, a viable function F1 is a better function than another viable function F2 if I, ICSi (F1) ICSi (F2) There is no poor conversion order, and then [...]
- F1 and F2 are function template specialists, and according to the partial order rules described in the function template 14.5.5.2 for F1, F 2 is more specific than the template, or if not, then [...]
end Switch to, here is hosting an implicit conversion that 13.3.3.1.1 / can participate in a standard conversion sequence on the 3 /
.
Comments
Post a Comment