fork download
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. struct ReIm
  5. {
  6. const ReIm* operator ->() const { return this; }
  7. ReIm* operator ->() { return this; }
  8.  
  9. T re;
  10. T im;
  11. };
  12.  
  13.  
  14. struct Cplx
  15. {
  16. double x;
  17. double y;
  18.  
  19. ReIm<double> operator ->() const { return {x, y}; }
  20. ReIm<double&> operator ->() { return {x, y}; }
  21. };
  22.  
  23. int main() {
  24. Cplx c{4, 5};
  25.  
  26. c->re = 42;
  27. c->im = 51;
  28.  
  29. std::cout << c.x << " " << c.y << std::endl;
  30. }
  31.  
Success #stdin #stdout 0s 4516KB
stdin
Standard input is empty
stdout
42 51