fork(1) download
  1. #include <iostream>
  2.  
  3. class B;
  4.  
  5. class A {
  6. double d;
  7. public:
  8. A(double a) : d(a) {}
  9. friend class B;
  10. double operator*(B& b); // auto operator*(B& b) -> decltype(d * B::i) と書けない
  11. };
  12.  
  13. class B {
  14. int i;
  15. public:
  16. B(int j) : i(j) {}
  17. friend class A;
  18. auto operator*(A& a) -> decltype(i * A::d) {
  19. return i * a.d;
  20. }
  21. };
  22.  
  23. auto A::operator*(B& b) -> decltype(d * B::i)
  24. {
  25. return d * b.i;
  26. }
  27.  
  28. template<class T, class U>
  29. auto mul(T x, U y) -> decltype(x * y)
  30. {
  31. return x * y;
  32. }
  33.  
  34. int main()
  35. {
  36. A a(3.0);
  37. B b(5);
  38.  
  39. std::cout << mul(a, b) << std::endl;
  40. std::cout << a * b << std::endl;
  41. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
15
15