fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <utility>
  4. using namespace std;
  5.  
  6.  
  7. struct A
  8. {
  9. bool bValid;
  10. mutable bool singleTimeInvert;
  11. int value;
  12. bool isValid() { return bValid; }
  13.  
  14. A(bool b = false) : bValid(b), singleTimeInvert(false) {}
  15. A(int v) : bValid(true), singleTimeInvert(false), value(v) {}
  16. A(const A & a) : bValid(a.bValid), singleTimeInvert(a.singleTimeInvert), value(a.value) {}
  17. A(const A & a, bool i) : bValid(a.bValid), singleTimeInvert(i), value(a.value) {}
  18.  
  19. explicit operator bool() const
  20. {
  21. bool tmp = singleTimeInvert;
  22. singleTimeInvert = false;
  23. return tmp ? !bValid : bValid;
  24. }
  25.  
  26. A operator!() const
  27. {
  28. return A(*this, true);
  29. }
  30.  
  31. A& operator!=(const A & a)
  32. {
  33. *this = a;
  34. singleTimeInvert = true;
  35. return *this;
  36. }
  37.  
  38. };
  39.  
  40. A foo()
  41. {
  42. return 5;
  43. }
  44.  
  45. A bar()
  46. {
  47. return false;
  48. }
  49.  
  50. int main() {
  51.  
  52. if (auto a != foo())
  53. {
  54. cout<<"a is not valid\n";
  55. }
  56. else
  57. {
  58. cout<<"a = "<<a.value<<"\n";
  59. }
  60.  
  61. if (auto b != bar())
  62. {
  63. cout<<"b is not valid\n";
  64. }
  65. else
  66. {
  67. cout<<"b = "<<b.value<<"\n";
  68. }
  69.  
  70. return 0;
  71. }
Compilation error #stdin compilation error #stdout 0s 4308KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:52:6: error: expected primary-expression before ‘auto’
  if (auto a != foo())
      ^~~~
prog.cpp:52:6: error: expected ‘)’ before ‘auto’
prog.cpp:58:17: error: ‘a’ was not declared in this scope
   cout<<"a = "<<a.value<<"\n";
                 ^
prog.cpp:61:6: error: expected primary-expression before ‘auto’
  if (auto b != bar())
      ^~~~
prog.cpp:61:6: error: expected ‘)’ before ‘auto’
prog.cpp:67:17: error: ‘b’ was not declared in this scope
   cout<<"b = "<<b.value<<"\n";
                 ^
stdout
Standard output is empty