fork download
  1. #include <iostream>
  2.  
  3. class X
  4. {
  5. int value;
  6.  
  7. public:
  8.  
  9. X() : value(0) {};
  10.  
  11. X(int v) : value(v) {};
  12.  
  13. X(const X& x) { // Does not help!
  14. this->value = x.value;
  15. }
  16.  
  17. X(const X&& x) noexcept { // Does not help!
  18. this->value = x.value;
  19. }
  20.  
  21. X& operator=(const X& left) { // Does not help!
  22. value = left.value;
  23. return *this;
  24. };
  25.  
  26.  
  27. bool operator==(const X& right) const {
  28. return this->value == right.value;
  29. };
  30.  
  31. };
  32.  
  33. class Y
  34. {
  35. int value;
  36. public:
  37.  
  38. operator X() const {
  39.  
  40. return X(this->value);
  41.  
  42. }; // Y objects may be converted to X
  43. };
  44.  
  45. int main() {
  46.  
  47. X x1, x2;
  48. Y y1;
  49.  
  50. x1 == x2; // Compiles
  51.  
  52. auto newv = (X)y1; // Compiles
  53.  
  54. x1 == newv; // Accepted!
  55.  
  56. x1 == (X)y1; // Error!!!
  57.  
  58. } // END: main()
Success #stdin #stdout 0s 4516KB
stdin
Standard input is empty
stdout
Standard output is empty