fork download
  1. #include <cassert>
  2.  
  3. template<typename T>
  4. struct Exactly : T
  5. {
  6. explicit Exactly(T const& value) : T(value) {}
  7. };
  8.  
  9. class Complex
  10. {
  11. double re;
  12. double im;
  13.  
  14. public:
  15. Complex(double re, double im) : re(re), im(im) {}
  16.  
  17. // Non-members require either friend access or public getters.
  18. double real() const { return re; }
  19. double imag() const { return im; }
  20. };
  21.  
  22. #pragma GCC diagnostic push
  23. #pragma GCC diagnostic ignored "-Wfloat-equal"
  24. bool operator == (Complex const& a, Exactly<Complex> const& b)
  25. {
  26. return (a.real() == b.real()) && (a.imag() == b.imag());
  27. }
  28. // two more overloads for completeness
  29. bool operator == (Exactly<Complex> const& a, Complex const& b)
  30. {
  31. return (a.real() == b.real()) && (a.imag() == b.imag());
  32. }
  33. bool operator == (Exactly<Complex> const& a, Exactly<Complex> const& b)
  34. {
  35. return (a.real() == b.real()) && (a.imag() == b.imag());
  36. }
  37. #pragma GCC diagnostic pop
  38.  
  39. Exactly<Complex> exactlyOne(Complex(1.0, 0.0));
  40.  
  41. template<typename T>
  42. Exactly<T> exactly(T const& value)
  43. {
  44. return Exactly<T>(value);
  45. }
  46.  
  47. int main()
  48. {
  49. Complex c(1.0, 0.0);
  50. // assert( c == c ); // error (no match):
  51. assert( c == exactlyOne );
  52. assert( exactly(Complex(1.0, 0.0)) == c );
  53. assert( exactlyOne == exactlyOne );
  54. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty