On most compilers (I do not know any exceptions), exceptions are zero overhead when not thrown, but when exception is thrown unwinding stack is pretty slow (I've seen source claiming that penalty is x40 times).
For that reason many developers prefer use exceptions only for exceptional cases ;) (for things that hardly ever happen).
There is other way to meet your requirement, just use &&
operator and provide tool which can store status:
struct StatusKeeper {
bool update(Status newStatus) {
status = newStatus;
return status == Status::SUCCESS;
}
bool operator()(Status newStatus) {
return update(newStatus);
}
Status get() const {
return status;
}
operator Status () const {
return status;
}
private:
Status status = Status::SUCCESS;
};
Status BigFunction()
{
StatusKeeper status;
status(function1())
&& status(function2())
&& status(function3());
// or more verbose, but more readable:
status.update(function1())
&& status.update(function2())
&& status.update(function3());
return status;
}
Now second argument for operator &&
is evaluated only if first argument was evaluated to true
.
There is also possibility to use C macros for that purpose, but IMHO in C++ the less macros then better.