The noexcept keyword can be appropriately applied to many function signatures, but I am unsure as to when I should consider using it in practice. Based on what I have read so far, the last-minute addition of noexcept seems to address some important issues that arise when move constructors throw. However, I am still unable to provide satisfactory answers to some practical questions that led me to read more about noexcept in the first place.
There are many examples of functions that I know will never throw, but for which the compiler cannot determine so on its own. Should I append
noexceptto the function declaration in all such cases?Having to think about whether or not I need to append
noexceptafter every function declaration would greatly reduce programmer productivity (and frankly, would be a pain in the neck). For which situations should I be more careful about the use ofnoexcept, and for which situations can I get away with the impliednoexcept(false)?When can I realistically expect to observe a performance improvement after using
noexcept? In particular, give an example of code for which a C++ compiler is able to generate better machine code after the addition ofnoexcept.Personally, I care about
noexceptbecause of the increased freedom provided to the compiler to safely apply certain kinds of optimizations. Do modern compilers take advantage ofnoexceptin this way? If not, can I expect some of them to do so in the near future?
I think it is too early to give a "best practices" answer for this as there hasn't been enough time to use it in practice. If this was asked about throw specifiers right after they came out then the answers would be very different to now.
Well, then use it when it's obvious that the function will never throw.
It seems like the biggest optimization gains are from user optimizations, not compiler ones due to the possibility of checking
noexceptand overloading on it. Most compilers follow a no-penalty-if-you-don't-throw exception handling method, so I doubt it would change much (or anything) on the machine code level of your code, although perhaps reduce the binary size by removing the handling code.Using
noexceptin the big four (constructors, assignment, not destructors as they're alreadynoexcept) will likely cause the best improvements asnoexceptchecks are 'common' in template code such as instdcontainers. For instance,std::vectorwon't use your class's move unless it's markednoexcept(or the compiler can deduce it otherwise).