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

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

问题描述

noexcept 关键字可以适当地应用于许多函数签名,但我不确定何时应该考虑在实践中使用它。基于我到目前为止阅读的内容,最后一分钟添加 noexcept 似乎解决了移动构造函数抛出时出现的一些重要问题。但是,我仍然无法提供满意的答案一些实际问题,导致我更多地了解 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 some practical questions that led me to read more about noexcept in the first place.


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

考虑在每个函数声明之后是否需要添加 noexcept 会大大降低程序员的生产力(坦率地说,这将是一个痛苦)。对于哪种情况,我应该更加小心使用 noexcept ,对于哪些情况我可以用隐含的 noexcept(false)

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

我实际上希望在使用 noexcept ?特别是,给出一个代码示例,C ++编译器在添加 noexcept 后能够生成更好的机器代码。

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.

就个人而言,我关心 noexcept ,因为提供给编译器安全地应用某些类型的优化的自由度增加。现代编译器以这种方式利用 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?


推荐答案

我认为为此提供最佳实践答案为时过早,因为没有足够的时间在实践中使用它。如果这是在它们出来之后被询问关于throw说明符,那么答案将与现在非常不同。

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.


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

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.

使用 noexcept 后,我什么时候可以实际观察到性能提升? [...]个人而言,我关心 noexcept ,因为增加的自由度提供给编译器安全地应用某些类型的优化。

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.

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

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.

在大4(构造函数,赋值而不是析构函数)中使用 noexcept c $ c> noexcept )可能会导致最好的改进,因为 noexcept 检查在模板代码中是common,例如std容器。例如, std :: vector 不会使用你的类的移动,除非它标记为 noexcept 否则)。

Using noexcept in the big 4 (constructors, assignment, not destructors as they'll 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天全站免登陆