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 function f. 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 function f2 if arguments i, icsi(f1) not worse conversion sequence icsi(f2), , then

  • (1.3) argument j, icsj(f1) better conversion sequence icsj(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 sequence s2 if
    • (3.2.1) s1 proper subsequence of s2 (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

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -