我什么时候应该真的使用noexcept? [英] When should I really use noexcept?

查看:190
本文介绍了我什么时候应该真的使用noexcept?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

noexcept 关键字可以适用于许多功能签名,但我不确定何时应该考虑在实践中使用它。根据我迄今为止所读到的内容,最后添加的 noexcept 似乎解决了移动构造函数抛出时出现的一些重要问题。但是,我仍然无法提供一些实际问题的令人满意的答案,导致我更多地了解 noexcept


  1. 有很多函数的例子,我知道永远不会抛出,但编译器不能自己决定。我应该在所有这些情况下将 noexcept 附加到函数声明中?



    必须想想我是否需要在每个函数声明之后附加 noexcept 将大大降低程序员的生产力(坦率地说,这将是一个痛苦的屁股)。对于哪些情况,我应该更加谨慎的使用 noexcept ,以及哪些情况我可以避免隐含的 noexcept(false) code>?


  2. 在使用 noexcept 之后,我什么时候可以期待观察到性能提升? ?特别地,给出一个代码示例,在添加 noexcept 之后,C ++编译器能够生成更好的机器代码。



    个人而言,我关心 noexcept ,因为为编译器提供了更多的自由度,以安全地应用某些类型的优化。现代编译器以这种方式利用 noexcept 如果没有,我可以期望他们中的一些在不久的将来这样做吗?



解决方案>

我认为给一个最佳实践的答案为时尚早,因为在实践中没有足够的时间使用它。如果这个问题在出现之后被提出来,那么答案将会非常的不同。


在每个函数声明之后,我不需要附加 noexcept ,这将大大降低程序员的生产力(坦率地说,会是一个痛苦)。


然后使用它,显然该功能永远不会抛出。



<在$ noexcept 之后,我什么时候可以实际期望观察到性能提升? [...]个人来说,我关心 noexcept ,因为提供给编译器的安全性更高的安全地应用某些类型的优化。


似乎最大的优化收益来自用户优化,而不是编译器,因为可以检查 noexcept 并重载。大多数编译器遵循无罚款 - 如果你不要抛出异常处理方法,所以我怀疑它会改变代码的机器代码级别(或任何东西),尽管可能通过删除处理来减少二进制大小代码



在大4(构造函数,赋值,而不是析构函数)中使用 noexcept c $ c> noexcept )可能会导致最佳的改进,因为 noexcept 检查在模板代码中是常见的,例如在std容器中。例如, std :: vector 不会使用你的类的动作,除非它被标记为 noexcept (或者编译器可以推导出否则)。


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.

  1. 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)?

  2. 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?

解决方案

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.

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).

Well then use it when it's obvious that the function will never throw.

When can I realistically expect to observe a performance improvement after using noexcept? [...] Personally, I care about noexcept because the of increased freedom provided to the compiler to safely apply certain kinds of optimizations.

It seems like the biggest optimization gains are from user optimizations, not compiler ones due to possibility of checking noexcept and 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 noexcept in the big 4 (constructors, assignment, not destructors as they're already noexcept) will likely cause the best improvements as noexcept checks are 'common' in template code such as in std containers. For instance, std::vector won't use your class's move unless it's marked noexcept (or the compiler can deduce it otherwise).

这篇关于我什么时候应该真的使用noexcept?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆