I have several void functions (lets call them foo and bar) that share the same void function cleanup that, well, cleans up after them if they mess up:
#include <iostream>
void cleanup() { std::cout << "doing cleanup" << std::endl; }
void foo(int & i) {
if(i == 0) { return cleanup(); }
--i;
if(i == 0) { return cleanup(); }
++i;
}
void bar(int & i) {
if(i == 0) { return cleanup(); }
++i;
if(i == 0) { return cleanup(); }
--i;
}
int main() {
int i = 0;
foo(i);
bar(i);
return 0;
}
cpp.sh happily compiles and runs the code.
Thanks to the answer to this question I know that I can return an object of type void. But I don't know if it applies to returning void return values.
What do you think, does the code comply with the standard?
And would you rather return dummy integers to make the code more readable? Or would the useless return values make the code harder to read?
edit: I feel like I need to add some clarification as to why e.g. 'cleanup(); return;' is not a solution. The actual code is more complex than the example and depending on where I leave the function some other stuff happens after the cleanup() call. 'return cleanup();' is just a convenient way of not putting all the stuff behind it in conditionals in instances where I can/have to leave immediately.