noexcept关键字可以适当地应用于许多函数签名,但我不确定什么时候应该考虑在实践中使用它。根据我到目前为止读到的内容,最后一刻添加的noexcept似乎解决了move构造函数抛出时出现的一些重要问题。然而,对于一些实际问题,我仍然无法提供令人满意的答案,这些问题导致我首先更多地阅读了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 neck). 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?
在使用noexcept后,什么时候我可以实际地观察到性能的改善?特别地,请给出一个c++编译器在添加noexcept后能够生成更好的机器代码的代码示例。
嗯,永远退不了?从来没有时间?从来没有。
Noexcept用于编译器性能优化,与const用于编译器性能优化相同。也就是说,几乎从来没有。
Noexcept主要用于允许“you”在编译时检测函数是否可以抛出异常。记住:大多数编译器不会为异常发出特殊代码,除非它真的抛出了一些东西。所以noexcept并不是给编译器提示如何优化函数,而是给你提示如何使用函数。
像move_if_noexcept这样的模板将检测移动构造函数是否定义了noexcept,如果没有,则返回一个const&而不是该类型的&&。这是一种表示在非常安全的情况下移动的方式。
一般来说,你应该使用nono,除非你认为这样做确实有用。如果该类型的is_nothrow_constructible为真,则某些代码将采用不同的路径。如果你使用的代码会这样做,那么可以随意使用noexcept适当的构造函数。
简而言之:将它用于move构造函数和类似的构造,但不要觉得你必须用它发疯。
我知道有许多函数永远不会抛出,但编译器无法自行确定。在所有这些情况下,我应该在函数声明中添加noexcept吗?
当你说“我知道[它们]永远不会抛出”时,你的意思是通过检查函数的实现,你知道函数不会抛出。我认为这种方法是由内而外的。
It is better to consider whether a function may throw exceptions to be part of the design of the function: as important as the argument list and whether a method is a mutator (... const). Declaring that "this function never throws exceptions" is a constraint on the implementation. Omitting it does not mean the function might throw exceptions; it means that the current version of the function and all future versions may throw exceptions. It is a constraint that makes the implementation harder. But some methods must have the constraint to be practically useful; most importantly, so they can be called from destructors, but also for implementation of "roll-back" code in methods that provide the strong exception guarantee.
用Bjarne的话来说(c++编程语言,第4版,第366页):
Where termination is an acceptable response, an uncaught exception
will achieve that because it turns into a call of terminate()
(§13.5.2.5). Also, a noexcept specifier (§13.5.1.1) can make that
desire explicit.
Successful fault-tolerant systems are multilevel. Each level copes
with as many errors as it can without getting too contorted and leaves
the rest to higher levels. Exceptions support that view. Furthermore,
terminate() supports this view by providing an escape if the
exception-handling mechanism itself is corrupted or if it has been
incompletely used, thus leaving exceptions uncaught. Similarly,
noexcept provides a simple escape for errors where trying to recover
seems infeasible.
double compute(double x) noexcept; {
string s = "Courtney and Anya";
vector<double> tmp(10);
// ...
}
The vector constructor may fail to acquire memory for its ten doubles
and throw a std::bad_alloc. In that case, the program terminates. It
terminates unconditionally by invoking std::terminate() (§30.4.1.3).
It does not invoke destructors from calling functions. It is
implementation-defined whether destructors from scopes between the
throw and the noexcept (e.g., for s in compute()) are invoked. The
program is just about to terminate, so we should not depend on any
object anyway. By adding a noexcept specifier, we indicate that our
code was not written to cope with a throw.
这实际上(可能)对编译器中的优化器产生了巨大的影响。实际上,编译器通过函数定义后的空throw()语句以及适当的扩展已经拥有这个特性很多年了。我可以向你保证,现代编译器确实利用了这些知识来生成更好的代码。
几乎编译器中的每个优化都使用函数的“流图”来推断什么是合法的。流图通常由所谓的功能“块”(具有单一入口和单一出口的代码区域)和块之间的边组成,以指示流可以跳转到的位置。Noexcept更改流程图。
你要的是一个具体的例子。考虑下面的代码:
void foo(int x) {
try {
bar();
x = 5;
// Other stuff which doesn't modify x, but might throw
} catch(...) {
// Don't modify x
}
baz(x); // Or other statement using x
}
如果bar被标记为noexcept,则此函数的流图会有所不同(执行无法在bar的末尾和catch语句之间跳转)。当标记为noexcept时,编译器确定在baz函数期间x的值为5 - x=5块被认为“支配”了baz(x)块,没有从bar()到catch语句的边。
然后,它可以执行所谓的“常量传播”来生成更有效的代码。在这里,如果baz是内联的,那么使用x的语句也可能包含常量,然后过去的运行时求值可以转换为编译时求值,等等。
Anyway, the short answer: noexcept lets the compiler generate a tighter flow graph, and the flow graph is used to reason about all sorts of common compiler optimizations. To a compiler, user annotations of this nature are awesome. The compiler will try to figure this stuff out, but it usually can't (the function in question might be in another object file not visible to the compiler or transitively use some function which is not visible), or when it does, there is some trivial exception which might be thrown that you're not even aware of, so it can't implicitly label it as noexcept (allocating memory might throw bad_alloc, for example).
在使用noexcept后,什么时候我可以实际地观察到性能的改善?特别地,请给出一个c++编译器在添加noexcept后能够生成更好的机器代码的代码示例。
嗯,永远退不了?从来没有时间?从来没有。
Noexcept用于编译器性能优化,与const用于编译器性能优化相同。也就是说,几乎从来没有。
Noexcept主要用于允许“you”在编译时检测函数是否可以抛出异常。记住:大多数编译器不会为异常发出特殊代码,除非它真的抛出了一些东西。所以noexcept并不是给编译器提示如何优化函数,而是给你提示如何使用函数。
像move_if_noexcept这样的模板将检测移动构造函数是否定义了noexcept,如果没有,则返回一个const&而不是该类型的&&。这是一种表示在非常安全的情况下移动的方式。
一般来说,你应该使用nono,除非你认为这样做确实有用。如果该类型的is_nothrow_constructible为真,则某些代码将采用不同的路径。如果你使用的代码会这样做,那么可以随意使用noexcept适当的构造函数。
简而言之:将它用于move构造函数和类似的构造,但不要觉得你必须用它发疯。