Since noexcept
was introduced and addressed many of the problems with throw()
, there is now a reason for specifying whether a function does not throw an exception. While it is not a compile time restriction, it will enable some potential optimizations by the compiler, and if nothing else, a signalling, and documentation, value for the user of the function.
In light of this I was actually a bit surprised, when I browsed through the standard functions such as: std::time
, std::timespec_get
, and std::memcmp
, and saw that none of them used the noexcept
specifier. It is not like there are no functions at all using it, in the standard library, for instance the std::tie
function uses it, and others as well. But a large portion of functions, do not use it.
I guess it makes sense for functions that have undefined behaviour in certain situations, such as std::strlen
, since this will allow implementers more freedom.
But for functions where there are no specified situations which can lead to undefined behaviour, and which obviously do not throw exceptions, why would these functions not be declared using the noexcept
specifier?
It cannot only be due to keeping similarity with the old C functions, since for instance std::timespec_get
is a new function in C++11, so it must be for some other reason.
The compiler might very well be smart enough to detect that the functions will not throw exceptions, and as such be able to do the same optimizations. But in my opinion one of the best arguments for noexcept
is the documentation value, that comes from it, which will be lacking, when it is missing.
Which leads me to my final hypothesis, that the missing noexcept
specifiers in the standard library are really oversights, much like it was the case with the missing std::make_unique
. But unlike the missing std::make_unique
which was corrected (implemented) in C++14, the functions above, have still not (as of C++17) been made noexcept
.
Does anyone know the reasoning behind the missing noexcept
specifiers, or am I right in thinking that this is an oversight?
The primary use of noexcept is for generic algorithms, e.g., when resizing a std::vector<T> : for an efficient algorithm moving elements it is necessary to know ahead of time that none of the moves will throw. If moving elements might throw, elements need to be copied instead.
That noexcept keyword is tricky, but just know that if you use it, your coding world will spin faster.
The noexcept specifier with a Boolean parameter. The noexcept specifier has an optional Boolean parameter. noexcept(true) is equivalent to noexcept , meaning the function is non-throwing. noexcept(false) means the function is potentially throwing.
If you throw, your program terminates What happens if you throw from a noexcept function? Your program terminates, and may or may not unwind the stack. Terminating program isn't the nicest way to report an error to your user. You would be surprised that most of C++ code might throw.
Rephrasing what you wrote above,
how can you guarantee noexcept when things are passed by pointers?
When you write your custom classes/functions, you can mark things noexcept without no guarantee of throwing no exception. But you probably do not want to do it when you are not absolutely sure that your function will not throw in any case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With