fork(5) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. struct A
  6. {
  7. bool bValid;
  8. int value;
  9. bool isValid() { return bValid; }
  10.  
  11. A(bool b = false) : bValid(b) {}
  12. A(int v) : bValid(true), value(v) {}
  13.  
  14. operator bool() const
  15. {
  16. return bValid;
  17. }
  18.  
  19. };
  20.  
  21. A foo()
  22. {
  23. return 5;
  24. }
  25.  
  26. A bar()
  27. {
  28. return false;
  29. }
  30.  
  31. int main() {
  32.  
  33. if (auto a = foo())
  34. {
  35. cout<<"a = "<<a.value<<"\n";
  36. }
  37. else
  38. {
  39. cout<<"a is not valid\n";
  40. }
  41.  
  42. if (auto b = bar())
  43. {
  44. cout<<"b = "<<b.value<<"\n";
  45. }
  46. else
  47. {
  48. cout<<"b is not valid\n";
  49. }
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0s 4332KB
stdin
Standard input is empty
stdout
a = 5
b is not valid