fork download
  1. namespace NonTemplate
  2. {
  3. struct T1{};
  4.  
  5. struct T2
  6. {
  7. operator T1(){ return T1(); }
  8. };
  9.  
  10. T2 operator*(const T1&, const T1&){ return T2(); }
  11. }
  12.  
  13. namespace Template
  14. {
  15. template <typename T>
  16. struct T1{};
  17.  
  18. template <typename T>
  19. struct T2
  20. {
  21. operator T1<T>(){ return T1<T>(); }
  22. };
  23.  
  24. template <typename T>
  25. T2<T> operator*(const T1<T>&, const T1<T>&){ return T2<T>(); }
  26.  
  27. // Раскомментировать вот эту строчку и ошибки не будет
  28. // T2<int> operator*(const T1<int>&, const T1<int>&){ return T2<int>(); }
  29. }
  30.  
  31. int main()
  32. {
  33. {
  34. NonTemplate::T1 t1;
  35. NonTemplate::T2 t2;
  36. t1 * t2; // ok
  37. }
  38. {
  39. Template::T1<int> t1;
  40. Template::T2<int> t2;
  41. t1 * static_cast<Template::T1<int>>(t2); // ok
  42. t1 * t2; // error (я где-то косячу в объявлении шаблонов?)
  43. }
  44. }
Compilation error #stdin compilation error #stdout 0s 3452KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:42:6: error: no match for 'operator*' (operand types are 'Template::T1<int>' and 'Template::T2<int>')
   t1 * t2; // error (я где-то косячу в объявлении шаблонов?)
      ^
prog.cpp:25:8: note: candidate: template<class T> Template::T2<T> Template::operator*(const Template::T1<T>&, const Template::T1<T>&)
  T2<T> operator*(const T1<T>&, const T1<T>&){ return T2<T>(); }
        ^
prog.cpp:25:8: note:   template argument deduction/substitution failed:
prog.cpp:42:8: note:   'Template::T2<int>' is not derived from 'const Template::T1<T>'
   t1 * t2; // error (я где-то косячу в объявлении шаблонов?)
        ^
stdout
Standard output is empty