fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Test
  5. {
  6. public:
  7. Test(bool condition) : condition(condition)
  8. {
  9. if(condition)
  10. {
  11. std::cout << "if(condition) special init needs" << std::endl;
  12. }
  13. }
  14.  
  15. ~Test()
  16. {
  17. if(condition)
  18. {
  19. std::cout << "if(condition) special cleanup needs" << std::endl;
  20. }
  21. }
  22.  
  23. operator bool() { return condition; }
  24.  
  25. private:
  26. bool condition = false;
  27. };
  28.  
  29. #define DoIf(cond) if(Test DoIf_test = Test(cond))
  30.  
  31. int main()
  32. {
  33. std::cout << "Start of main" << std::endl;
  34.  
  35. DoIf(true)
  36. {
  37. std::cout << "Inside condition A" << std::endl;
  38. }
  39.  
  40. DoIf(false)
  41. {
  42. std::cout << "Inside condition B" << std::endl;
  43. }
  44.  
  45. std::cout << "End of main" << std::endl;
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Start of main
if(condition) special init needs
Inside condition A
if(condition) special cleanup needs
End of main