fork download
  1. #include <iostream>
  2.  
  3. struct complex_data
  4. {
  5. constexpr complex_data(double re, double im = 0) : re(re), im(im) {}
  6.  
  7. double re = 0; // real part
  8. double im = 0; // immaginary part
  9. };
  10.  
  11. constexpr complex_data operator"" _i(unsigned long long d) { return complex_data{ 0.0, static_cast<double>(d) }; }
  12.  
  13. constexpr complex_data operator+(const complex_data& lhs, const complex_data& rhs)
  14. {
  15. return complex_data{ lhs.re + rhs.re, lhs.im + rhs.im };
  16. }
  17.  
  18. std::ostream& operator<<(std::ostream& os, const complex_data& c) { return os << c.re << " + " << c.im << "i"; }
  19.  
  20. int main()
  21. {
  22. complex_data c = 3 + 2_i;
  23.  
  24. std::cout << c << std::endl;
  25. }
  26.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
3 + 2i