fork download
  1. #include <utility>
  2.  
  3. struct my_complex
  4. {
  5. float r, i;
  6.  
  7. my_complex(float r, float i)
  8. : r(r), i(i)
  9. {}
  10.  
  11. template <typename Expr>
  12. my_complex(Expr expr)
  13. {
  14. auto result = expr();
  15. r = result.r;
  16. i = result.i;
  17. }
  18. };
  19.  
  20. struct addition
  21. {
  22. my_complex const& lhs;
  23. my_complex const& rhs;
  24.  
  25. addition(my_complex const& lhs, my_complex const& rhs)
  26. : lhs(lhs), rhs(rhs)
  27. {}
  28.  
  29. my_complex operator () ()
  30. {
  31. return my_complex(lhs.r + rhs.r, lhs.i + rhs.i);
  32. }
  33. };
  34.  
  35. addition operator + (my_complex const& lhs, my_complex const& rhs)
  36. {
  37. return addition(lhs, rhs);
  38. }
  39.  
  40. int main()
  41. {
  42. my_complex a(42.f, 666.f);
  43. my_complex b(3.14f, 2.7f);
  44.  
  45. auto c = a + b;
  46.  
  47. std::swap(a, c);
  48. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:47:23: error: no matching function for call to 'swap(my_complex&, addition&)'
stdout
Standard output is empty