C ++ Function Template instantiaion with implicit parameters

I cannot understand why the following code compiles:

#include <iostream>                                                                 

void bar(int x) {                                                                   
  std::cout << "int " << x << std::endl;                                            
}                                                                                   

void bar(double x) {                                                                
  std::cout << "double " << x << std::endl;                                         
}                                                                                   

template <typename A, typename B>    // Note the order of A and B.                                               
void foo(B x) {                                                                     
  bar((A)x);                                                                        
}                                                                                   

int main() {                                                                        
  int x = 1;                                                                        
  double y = 2;                                                                     

  foo<int>(x);        // Compiles OK.                                                              
  foo<double>(y);     // Compiles OK.                                               

  return 0;                                                                         
}

But if I switch the order Aand B, as shown below, it will not compile:

#include <iostream>                                                                 

void bar(int x) {                                                                   
  std::cout << "int " << x << std::endl;                                            
}                                                                                   

void bar(double x) {                                                                
  std::cout << "double " << x << std::endl;                                         
}                                                                                   

template <typename B, typename A>    // Order of A and B are switched.
void foo(B x) {                                                                     
  bar((A)x);                                                                        
}                                                                                   

int main() {                                                                        
  int x = 1;                                                                        
  double y = 2;                                                                     

  foo<int>(x);        // error: no matching function for call to ‘foo(int&)’
  foo<double>(y);     // error: no matching function for call to ‘foo(double&)’                                                              

  return 0;                                                                         
}      

EDIT: Special clarifications are welcome, but it would be better if someone could specify exactly what the spec is. He speaks. Thank!

+5
source share
1 answer

, A - int, foo<int>, , B int - , , , A B ( : A , B).

, B , A , , A, . , B foo<int>, B, int, B, A, , .

, . A ?

, , , .

+7

All Articles