fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3.  
  4. using namespace std;
  5.  
  6. template<typename T1, typename T2>
  7. auto add(T1 l, T2 r) -> decltype(l + r){
  8. return l + r;
  9. }
  10.  
  11. class C {};
  12.  
  13. class B {};
  14.  
  15. class A {
  16. public:
  17. C operator+(const B& b) {
  18. C c;
  19. return c;
  20. }
  21. };
  22.  
  23.  
  24. int main() {
  25. // Using add()
  26. A a;
  27. B b;
  28. auto c = add(a, b);
  29.  
  30. cout << typeid(a).name() << endl;
  31. cout << typeid(b).name() << endl;
  32. cout << typeid(c).name() << endl;
  33. cout << endl;
  34.  
  35. // Doing the same thing but not on a function
  36. A a2;
  37. B b2;
  38. auto c2 = a2 + b2;
  39.  
  40. cout << typeid(a2).name() << endl;
  41. cout << typeid(b2).name() << endl;
  42. cout << typeid(c2).name() << endl;
  43. }
  44.  
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
1A
1B
1C

1A
1B
1C