Sometimes your compiler cannot deduce that your function actually has no missing return. In such cases, there are several solutions:
Suppose the following simplified code (although modern compilers will see that there is no path leak, just an example):
if (foo == 0) { return bar; } else { return frob; }
Change code structure
if (foo == 0) { return bar; } return frob;
This works well if you can interpret the if statement as a kind of firewall or prerequisite.
interruptions ()
if (foo == 0) { return bar; } else { return frob; } abort(); return -1;
Return something else accordingly. The commentary tells about the programmers and themselves, why this is.
quit
#include <stdexcept> if (foo == 0) { return bar; } else { return frob; } throw std::runtime_error ("impossible");
Disadvantages of a single exit exit point
control flow
Some return to the one-inverse-function-function aka with one function-exit point as a workaround. This can be considered deprecated in C ++ because you almost never know where the function will actually exit:
void foo(int&); int bar () { int ret = -1; foo (ret); return ret; }
It looks good and looks like SFEP, but reverse engineering a third-party proprietary libfoo shows:
void foo (int &) { if (rand()%2) throw ":P"; }
This argument is not valid if bar() nothrow and therefore can only call nothrow functions.
complexity
Each volatile variable increases the complexity of your code and increases the brain load of your supporting code. This means that more code and more states to check and verify, in turn, means that you suck out more states from the brains of the maintenance staff, which in turn means that less guardian brain power remains for important material.
missing default constructor
Some classes do not have a default, and you will have to write really dummy code, if at all possible:
File mogrify() { File f ("/dev/random");
Itβs hack enough to declare it.