Noexcept and JUCE

Hello guys !

Stupid question of the day : I have reworked a little all the source code I have been writing these last two years, and I try to follow the coding standards of JUCE as much as possible. I have got also some inspiration from the demo projects and the JUCE source code.

I have seen that the keyword "noexcept" is used a lot in the source code, and it is suggested to include it on each function declaration, with potential 10x increase in performance, when the function isn't supposed to throw exceptions. Since I use jassert a lot, and almost never use try / throw / catch in my code, I assume I should add a lot noexcept in my code.

However, I don't get what is the real influence of this keyword, and how it might increase so much the performance. I have tried to look for talks about this on Google, but I have only found confusing statements and arguments.

So, someone can enlighten me ? Thanks in advance !

Any information about this ? Thanks in advance ;)

I highly doubt "noexcept" regularly results in performance boosts, but if the chance is there, why not try taking an advantage of it?

IIRC noexcept also has the benefit it will make the program crash right away if you throw an exception regardless from a function that is marked noexcept. So it's kind of an enforcement for really keeping exceptions away from your code. (edit : Just tested this, at least in VS2015 it works that way. A function that is marked noexcept that throws an exception causes abort() to be called even if there's an exception handler in the calling code.)

Scott Meyers has a nice chapter about noexcept in his "Effective Modern C++" book (draft version of that chapter available here).

Basically, the bottom line is that noexcept in C++11/14 often results in faster code than throw() in C++98, at the cost of less guarantees about what will happen if you throw an exception inside such a function (which hopefully won't happen to you anyway).

Also, compared to a function that doesn't have a throw/noexcept declaration at all,  the performance boost of declaring a function noexcept can sometimes be very significant.

Also, it's worth to notice that in JUCE, noexcept is a macro defined as throw() when using compilers that don't support noexcept properly (for backwards compatibility). So there are several advantages to use it and no disadvantage.

Thanks for the information guys !

I have just seen also that Visual Studio officially supports this keyword only from its 2015 version... I should update from Visual Studio 2013 soon I guess !

Yes, you should. As already said, JUCE will turn noexcept into throw() on older Visual Studio versions, which will work fine, but might have worse performance in a few cases.

There's also a recent video talking about the same thing as the Meyers book:
although it's in the context of explaining type traits.
If a move contructor or move assignment operator is declared noexcept or throw(), then it can be used in a move operation without the worry that it will emit an exception and thus be undo-able. Therefore with noexcept the strong exception guarantee can be satified and so the move operation is allowed. Otherwise it would fall back to a copy operation which is slower. So the end result is about better performance by allowing move operations.