fork download
  1. #include <complex>
  2. #include <functional>
  3. #include <utility>
  4.  
  5. template <class A, class B>
  6. class X {
  7. public:
  8. A a;
  9. B b;
  10.  
  11. X(A a, B b) : a(a), b(b) {}
  12. X(const std::pair<A, B> &value) : a(value.first), b(value.second) {}
  13.  
  14.  
  15. template <class C,template <typename, typename> typename O >
  16. friend X<A, C> operator+(const X<A, B> &left, const O<B, C> &right) {
  17. return left + X<B,C>(right);
  18. }
  19. template <class C>
  20. friend X<A, C> operator+(const X<A, B> &left, const X<B, C> &right) {
  21. return X<A, C>(left.a,right.b);
  22. }
  23. };
  24.  
  25.  
  26. int main() {
  27. X<int, int> a(1, 2);
  28. X<int, float> b(1, 2.5f);
  29. std::pair<int, float> c(1, 2.5f);
  30.  
  31. a + b; // Valid
  32. a + c; // Works
  33. }
Success #stdin #stdout 0s 4528KB
stdin
Standard input is empty
stdout
Standard output is empty