Is there a problem in declaring many noexcept functions?

3

I see many C ++ code in which the programmer is sure that the function is going to throw an exception, but does not tell this to the compiler, perhaps because it can cause some problem in reporting many functions like noexcept? Is there any problem in setting many functions like noexcept? If so, which ones?

    
asked by anonymous 27.11.2018 / 02:45

1 answer

3

noexcept is a tool with a specific purpose to allow 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 std::vector move its elements if your move constructors are noexcept .

Tagging every function with noexcept is probably not a good idea. The overuse and purposeless use of a tool does not bring benefits, on the contrary. The noexcept specifier works like a contract: it adds it to functions that you are absolutely sure will not throw (reads throw ) an exception, and that will never be changed to possibly launchable, or if you definitely want It is assumed that std::terminate is called if an exception occurs.

But that does not mean adding noexcept to any and all functions arbitrarily.

However, you can 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 full of throw s.     

27.11.2018 / 03:53