Noexcept practice for style and performance?

I started adding noexcept to my code, but I'm wondering if I should even add it to the built-in functions. I assume that the optimizer omits the runtime check when it is clearly not needed ... but from a human / style point of view, is it worth adding extra trivial functions such as getters, settings, zoom functions, etc.? I think this is a visual mess for something completely obvious. I discuss the rule that inline functions get to omit noexcept, but regular .hpp / .cpp functions should have this if they don't drop.

Secondly, I have a large amount of code that cannot be thrown away at all, because it does not have allocations (in my chess engine) that does not contain STL or anything else that can fail, so success is always guaranteed. Is this a possible slowdown due to runtime checking? Does anyone use a macro to switch between using noexcept for DEBUG assemblies, but replace with throw() for a release that is only a compilation?

+4
source share
2 answers

If your built-in function is a sheet-level function, that is, it does not call any functions, then theoretically the compiler can determine that it will not throw and omit any exception handling that could otherwise be generated. Thus, this may not be necessary.

Having said that, you should not expect performance noexcept from adding noexcept . Whichever code that was supposed to be created to handle exception propagation should not become more complex by adding noexcept . It is worth noting that the compiler is allowed to completely abandon the stack if an exception is thrown from the noexcept function. This is largely due to the direct benefits of noexcept .

Regarding style recommendations, first of all, consider whether noexcept will be a useful part of your interface. Things like move operations can greatly benefit from noexcept for algorithmic reasons, but apart from that you really need to decide where noexcept matters to you, your interface, and the users of your interface.

If this does not answer your question, feel free to comment on my answer, and I will clarify further.

Sidenote: throw() , as well as deprecated in C ++ 11, does not provide the same guarantees as noexcept . If an exception is thrown through the declared throw() function, the stack must be completely unwound to the caller of this function. See 15.5.2.1 in the N3337 version of the C ++ standard for a reference to this behavior.

+4
source

By adding noxcept or noexcept (true) for the function, ask the compiler to add runtime checking and call std :: terminate. So you have little success. You must get something from this, otherwise I do not see any point. The standard library has the following features:

 is_nothrow_constructible, is_nothrow_default_constructible, is_nothrow_move_constructible, is_nothrow_copy_constructible, is_nothrow_assignable, is_nothrow_move_assignable, is_nothrow_copy_assignable, is_nothrow_destructible. 

Containers in the standard library are known to use these properties for contained types for optimization (moving instead of copying). Perhaps some other optimizations that I don't know about. It makes sense for me to add a noexcept specifier to the appropriate constructor or assignment operator (of course, if they don't really throw away), that these traits will return true and get better performance when using your class with standard containers.

I don’t think adding noexcept to a regular function like getter or setter is a good idea: you get a performance hit and get nothing. If your code does not throw and does not use the standard library - my advice: do not use noexcept at all.

0
source

All Articles