简体   繁体   English

noexcept说明符和编译器优化

[英]noexcept specifier and compiler optimizations

I have read unclear things regarding the noexcept specifier and compiler optimizations. 我已经阅读了关于noexcept说明符和编译器优化的不清楚的事情。 When specifying noexcept the compiler may optimize: 指定noexcept ,编译器可能会优化:

  • Compile time (faster compilation). 编译时间(编译速度更快)。
  • Execution time (code runs faster). 执行时间(代码运行得更快)。
  • Or both? 或两者?
  • Or none? 还是没有?

The original reason for noexpect was to enable libraries to use faster move-constructors internally, if the calling function is not allowed to throw by specification. noexpect的最初原因是如果不允许调用函数按规范抛出,则允许库在内部使用更快的move-constructors。

Next, big performance optimizations can be achieved in containers like STL vector when your type's move constructor and move assignment are annotated with noexcept. 接下来,当您的类型的移动构造函数和移动赋值使用noexcept注释时,可以在容器(如STL向量)中实现大的性能优化。 When STL utility std::move_if_noexcept detects that your moves do not throw it will use these safe moves rather than copies for some operations (like resize). 当STL实用程序std :: move_if_noexcept检测到您的移动没有​​抛出它时,将使用这些安全移动而不是复制某些操作(如调整大小)。 This, in case of containers storing millions of elements, will enable huge optimizations. 在容器存储数百万个元素的情况下,这将实现巨大的优化。

(quoted from using-noexcept ) (引自using-noexcept


Additionally, the compiler does not have to generate extra code for stack-unwinding, if it knows that no exceptions can be thrown due to a noexpect specifier. 此外,如果编译器知道由于noexpect说明符而不能抛出任何异常,则编译器不必为堆栈展开生成额外的代码。


I can't see how compile-time is substantially impacted by noexcept-specifiers. 我看不出编译时如何受到noexcept-specifiers的严重影响。 The resulting runtime can be a lot faster though. 但是,生成的运行时可以快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM