c++ - Why is template overload a better match than a simple conversion? -
#include <iostream> using namespace std; template<typename t> void func(t t) { std::cout << "matched template\n"; } void func(long x) { std::cout << "matched long\n"; } int main() { func(0); }
output:
matched template
in other cases, non-template function preferred when overload resolution might ambiguous, why 1 different?
§13.3.3 [over.match.best]/p1-2:
1 define
icsi(f)
follows:
- (1.1) [inapplicable bullet omitted]
- (1.2) let
icsi(f)
denote implicit conversion sequence converts i-th argument in list type of i-th parameter of viable functionf
. 13.3.3.1 defines implicit conversion sequences , 13.3.3.2 defines means 1 implicit conversion sequence better conversion sequence or worse conversion sequence another.given these definitions, viable function
f1
defined better function viable functionf2
if argumentsi
,icsi(f1)
not worse conversion sequenceicsi(f2)
, , then
(1.3) argument
j
,icsj(f1)
better conversion sequenceicsj(f2)
, or, if not that,[several inapplicable bullets omitted]
- (1.6)
f1
not function template specialization ,f2
function template specialization, or, if not that,- [inapplicable bullet omitted]
2 if there 1 viable function better function other viable functions, 1 selected overload resolution; otherwise call ill-formed.
§13.3.3.2 [over.ics.rank], bullet 3.2:
- (3.2) standard conversion sequence
s1
better conversion sequence standard conversion sequences2
if
- (3.2.1)
s1
proper subsequence ofs2
(comparing conversion sequences in canonical form defined 13.3.3.1.1, excluding lvalue transformation; identity conversion sequence considered subsequence of non-identity conversion sequence)
let f1 = func<int>(int)
, f2 = func(long)
, there's 1 argument, of type int
. ics1(f1)
identity conversion; ics1(f2)
integer conversion int
long
; therefore ics1(f1)
better conversion sequence ics1(f2)
per [over.ics.rank]/3.2.1 (and definition not worse ics1(f2)
). per bullet 1.3 in [over.match.best], f1 better f2. template/non-template tiebreaker, in bullet 1.6, never comes play.
Comments
Post a Comment