fork download
  1. #include <iostream>
  2.  
  3. class X { };
  4.  
  5. template <class T>
  6. class Mistake {
  7. public:
  8. T x;
  9.  
  10. Mistake(const T& k) : x(k) { }
  11. Mistake(const X&) : x(1) { }
  12.  
  13. void print() { std::cout << x << std::endl; }
  14. };
  15.  
  16. template <class T>
  17. Mistake<T> operator+(const Mistake<T>& a, const Mistake<T>& b) {
  18. return Mistake<T>(a.x + b.x);
  19. }
  20.  
  21. template <class T, class U>
  22. Mistake<T> operator+(const Mistake<T>& a, U const& b) {
  23. return a + static_cast<Mistake<T>>(b);
  24. }
  25.  
  26. template <class T, class U>
  27. Mistake<T> operator+(const U& a, Mistake<T> const& b) {
  28. return static_cast<Mistake<T>>(a) + b;
  29. }
  30.  
  31. int main()
  32. {
  33. X a, b;
  34. Mistake<int> foo = static_cast<Mistake<int>>(a) + static_cast<Mistake<int>>(b);
  35. foo = Mistake<int>(a) + b;
  36. foo = a + Mistake<int>(b);
  37. }
  38.  
Success #stdin #stdout 0s 2892KB
stdin
Standard input is empty
stdout
Standard output is empty