noreturn
suggest changeC++11 introduced the [[noreturn]] attribute. It indicates that a function does not return to the caller, either by executing a return statement or by reaching the end of its body. A void function still returns to the caller; it merely has no value to return. A [[noreturn]] function may end by calling std::terminate or std::exit, or by throwing an exception. It may also leave via longjmp.
For instance, the function below will always either throw an exception or call std::terminate, so it is a good candidate for [[noreturn]]:
[[noreturn]] void ownAssertFailureHandler(std::string message) {
std::cerr << message << std::endl;
if (THROW_EXCEPTION_ON_ASSERT)
throw AssertException(std::move(message));
std::terminate();
}
This kind of functionality allows the compiler to end a function without a return statement if it knows the code will never be executed. Here, because the call to ownAssertFailureHandler (defined above) in the code below will never return, the compiler does not need to add code below that call:
std::vector<int> createSequence(int end) {
if (end >= 0) {
std::vector<int> sequence;
sequence.reserve(end+1);
for (int i = 0; i <= end; ++i)
sequence.push_back(i);
return sequence;
}
ownAssertFailureHandler("Negative number passed to createSequence()");
// return std::vector<int>{}; //< Not needed because of [[noreturn]]
}
It is undefined behavior if the function will actually return, so the following is not allowed:
[[noreturn]] void assertPositive(int number) {
if (number >= 0)
return;
else
ownAssertFailureHandler("Positive number expected"); //< [[noreturn]]
}
Note that the [[noreturn]] is mostly used in void functions. However, this is not a requirement, allowing the functions to be used in generic programming:
template<class InconsistencyHandler>
double fortyTwoDivideBy(int i) {
if (i == 0)
i = InconsistencyHandler::correct(i);
return 42. / i;
}
struct InconsistencyThrower {
[[noreturn]] static int correct(int i) { ownAssertFailureHandler("Unknown inconsistency"); }
};
struct InconsistencyChangeToOne {
static int correct(int i) { return 1; }
};
double fortyTwo = fortyTwoDivideBy<InconsistencyChangeToOne>(0);
double unreachable = fortyTwoDivideBy<InconsistencyThrower>(0);
The following standard library functions have this attribute:
- std::abort
- std::exit
- std::quick_exit
- std::unexpected (removed in C++17)
- std::terminate
- std::rethrow_exception
- std::throw_with_nested
- std::nested_exception::rethrow_nested