Когда я должен действительно использовать noexcept?

noexcept Ключевое слово может быть соответствующим образом применено ко многим сигнатурам функций, но я не уверен относительно того, когда мне следует рассмотреть возможность использования его на практике. Исходя из того, что я прочитал, добавление в последнюю минутуnoexcept кажется, решает некоторые важные проблемы, возникающие при броске конструкторов перемещения. Тем не менее, я все еще не могу дать удовлетворительные ответы на некоторые практические вопросы, которые побудили меня прочитать больше оnoexcept на первом месте.

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 noexcept to the function declaration in all such cases?

Having to think about whether or not I need to append noexcept after every function declaration would greatly reduce programmer productivity (and frankly, would be a pain in the ass). For which situations should I be more careful about the use of noexcept, and for which situations can I get away with the implied noexcept(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 of noexcept.

Personally, I care about noexcept because of the increased freedom provided to the compiler to safely apply certain kinds of optimizations. Do modern compilers take advantage of noexcept in this way? If not, can I expect some of them to do so in the near future?

Ответы на вопрос(8)

Ваш ответ на вопрос