Suppose you have a function that usually cannot fail, for example:
std::string convert_integer_to_string(int x);
In the proposal, this will be a candidate for noexcept . However, the implementation most likely includes dynamic memory management, so when allocating memory using the new operator, it can always call std :: bad_alloc .
Is it recommended to annotate a function as noexcept?
From a practical point of view, it is extremely difficult to cope with insufficient memory situations in a reasonable way. Most programs simply assume that there is enough memory. Calling std::terminate , as it would if the noexcept function throws std::bad_alloc , in this case seems reasonable.
For me, noexcept is some form of documentation. This is a promise that you (or the optimizer) can safely assume that this function will never throw. If you are programming an application that does not care about out-of-memory situations, it is still a valid assumption.
I believe the safest recommendation is to never use noexcept if the std::bad_alloc exception can be std::bad_alloc . On the other hand, I am wondering if there are any advantages to using noexcept anyway, assuming you are not worried about a memory shortage situation (i.e. if std::terminate is ok).
source share