fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. #define CALL_TILL_FALIURE sequence_call_till_failure()
  5.  
  6. class sequence_call_till_failure
  7. {
  8. bool success;
  9. public:
  10. sequence_call_till_failure() : success(false) { }
  11.  
  12. template < typename Func, typename... Args >
  13. sequence_call_till_failure&
  14. operator()( Func func, Args... args ) {
  15.  
  16. if( !success )
  17. success = success || func( std::forward<Args>(args)... );
  18.  
  19. return *this;
  20. }
  21.  
  22. operator bool() const {
  23. return success;
  24. }
  25. };
  26.  
  27. bool PlanA() { std::cout << __FUNCTION__ << std::endl; return false; }
  28. bool PlanB() { std::cout << __FUNCTION__ << std::endl; return false; }
  29. bool PlanC() { std::cout << __FUNCTION__ << std::endl; return false; }
  30. bool is_odd( int value ) { std::cout << __FUNCTION__ << std::endl; return value % 2 == 1; }
  31. int add( int lhs, int rhs ) { std::cout << __FUNCTION__ << std::endl; return lhs + rhs; }
  32. bool Error() { std::cout << __FUNCTION__ << std::endl; return false; }
  33.  
  34. int main() {
  35.  
  36. using namespace std;
  37.  
  38. bool result = CALL_TILL_FALIURE( PlanA )( PlanB )( PlanC )( is_odd, 2 )( add, 2, 3 )( Error );
  39.  
  40. cout << result << endl;
  41. }
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
PlanA
PlanB
PlanC
is_odd
add
1