如何描述一个在执行过程中抛出异常时不会产生副作用的方法?
我只是不记得用于此属性和其他相关属性的术语。
编辑-也许这样的概念不存在,但我记得读过Effective C++(或更有效的C++)中的一些内容,其中他提倡使用交换在函数中最后提交更改,因为向量交换函数保证不会抛出异常。
I just can't remember the terminology used for this and other related properties.
EDIT - Maybe such a concept doesn't exist but I remember reading something in Effective C++ (or More Effective C++) where he advocated using swaps to commit changes last in the function because the vector swap functions were guaranteed not to throw an exception.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你的意思是说该函数是“异常安全的”。 请参阅http://en.wikipedia.org/wiki/Exception_safety。
维基百科文章进一步将安全性分为不同级别。 这是与这里相关的:
有一个对 STL 设计文档的引用,介绍了异常安全和提交或回滚语义: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1997/N1077.asc
I think you mean to say that the function is "exception-safe". See e.g. http://en.wikipedia.org/wiki/Exception_safety.
The Wikipedia article further divides the safety into various levels. This is the one that is relevant here:
There's a reference to an STL design document that introduces exception safety and commit-or-rollback semantics: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1997/N1077.asc
您可能会想到例外保证,特别是“强保证”。
You might be thinking of an exception guarantee, in particular the "strong guarantee".
阅读您的编辑后,我怀疑您的标题略有不正确。 你真的意味着它没有副作用无论是否抛出异常(根据当前的措辞)或者当抛出异常时它没有副作用,但如果方法完成时没有抛出异常一个例外那么它会/可能有副作用吗? 有一个相当大的区别:)
后者在 Accelerated C# 2008 中被描述为“异常中立”。
Having read your edit, I suspect your title is slightly incorrect. Do you really mean it has no side effects whether or not an exception is thrown (as per the current wording) or that it has no side effects when an exception is thrown, but if the method completes without throwing an exception then it will/can have side effects? There's a pretty big difference :)
The latter is described in Accelerated C# 2008 as "exception neutrality".