Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is noexcept not used more in the standard library?

Tags:

c++

c++11

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?

like image 689
Tommy Andersen Avatar asked Jun 07 '17 19:06

Tommy Andersen


People also ask

Is Noexcept useful?

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.

Does Noexcept make code faster?

That noexcept keyword is tricky, but just know that if you use it, your coding world will spin faster.

What is Noexcept false?

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.

What happens when Noexcept throws?

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.


1 Answers

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.

like image 142
kmkim85 Avatar answered Sep 18 '22 19:09

kmkim85