Создание экземпляра шаблона функции C++ с неявными параметрами

Я не могу понять, почему следующий код компилируется нормально:

#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;                                                                         
}

Но если я поменяю порядок Aи B, как показано ниже, он не скомпилируется:

#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;                                                                         
}      

РЕДАКТИРОВАТЬ :Специальные -пояснения приветствуются, но было бы лучше, если бы кто-нибудь точно указал спецификацию. говорит. Спасибо!

5
задан fejese 10 January 2015 в 17:07
поделиться