Are there problems in declaring many noexcept functions?

Asked

Viewed 46 times

3

I see a lot of code in C++14/C++17 where the programmer is sure that the function will make an exception, but does not report this pro compiler, perhaps because it may cause some problem when reporting many functions like noexcept? Is there a problem in defining many functions as noexcept? If so, which ones?

1 answer

3


noexcept is a tool with a specific purpose of allowing certain optimizations, which would not be possible if exceptions had to be taken into account. The first optimization that comes into my head right now is the std::vector move your elements if your move constructors are noexcept.

Mark every function with noexcept probably not a good idea. Excessive and purposeless use of a tool does not bring benefits, on the contrary. The specifier noexcept works like a contract: adds it in functions that is absolutely sure that it will not launch (reads throw) an exception, and that will never be exchanged for possibly playable, or if definitely desired std::terminate is called if an exception occurs.

But that doesn’t mean adding noexcept in any function arbitrarily.

However, it is possible to disable C++ exceptions in compilers GCC, Clang and MSVC, rendering the use of noexcept redundant. It is necessary, however, to recompile the standard library to use it without exceptions, or simply abandon it, because it is filled with throws.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.