#include <iostream>
#include <functional>
#define CALL_TILL_FALIURE sequence_call_till_failure()
class sequence_call_till_failure
{
bool success;
public:
sequence_call_till_failure() : success(false) { }
template < typename Func, typename... Args >
sequence_call_till_failure&
operator()( Func func, Args... args ) {
if( !success )
success = success || func( std::forward<Args>(args)... );
return *this;
}
operator bool() const {
return success;
}
};
bool PlanA() { std::cout << __FUNCTION__ << std::endl; return false; }
bool PlanB() { std::cout << __FUNCTION__ << std::endl; return false; }
bool PlanC() { std::cout << __FUNCTION__ << std::endl; return false; }
bool is_odd( int value ) { std::cout << __FUNCTION__ << std::endl; return value % 2 == 1; }
int add( int lhs, int rhs ) { std::cout << __FUNCTION__ << std::endl; return lhs + rhs; }
bool Error() { std::cout << __FUNCTION__ << std::endl; return false; }
int main() {
using namespace std;
bool result = CALL_TILL_FALIURE( PlanA )( PlanB )( PlanC )( is_odd, 2 )( add, 2, 3 )( Error );
cout << result << endl;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KIAojZGVmaW5lIENBTExfVElMTF9GQUxJVVJFIHNlcXVlbmNlX2NhbGxfdGlsbF9mYWlsdXJlKCkKIApjbGFzcyBzZXF1ZW5jZV9jYWxsX3RpbGxfZmFpbHVyZQp7CiAgICBib29sIHN1Y2Nlc3M7CnB1YmxpYzogICAgCiAgICBzZXF1ZW5jZV9jYWxsX3RpbGxfZmFpbHVyZSgpIDogc3VjY2VzcyhmYWxzZSkgeyB9CiAgICAKICAgIHRlbXBsYXRlIDwgdHlwZW5hbWUgRnVuYywgdHlwZW5hbWUuLi4gQXJncyA+CiAgICBzZXF1ZW5jZV9jYWxsX3RpbGxfZmFpbHVyZSYKICAgIG9wZXJhdG9yKCkoIEZ1bmMgZnVuYywgQXJncy4uLiBhcmdzICkgewogICAgICAgIAogICAgICAgIGlmKCAhc3VjY2VzcyApCiAgICAgICAgICAgIHN1Y2Nlc3MgPSBzdWNjZXNzIHx8IGZ1bmMoIHN0ZDo6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