如何检测析构函数中的堆栈展开
我有一个简单的 C++ 对象,在函数 F()
的开头创建,以确保在 F()< 的开头和返回时调用两个匹配的函数(OpDo、OpUndo) /code>,通过使用对象的构造函数和析构函数。但是,我不希望在
F()
体内抛出异常时撤消操作。这可以干净地完成吗?我已经阅读过有关 std::uncaught_exception
的内容,但似乎不建议使用它。
I have a simple C++ object that I create at the start of function F()
to ensure two matched functions (OpDo, OpUndo) are called at the start and return of the F()
, by using the object's constructor and destructor. However, I don't want the operation to be undone in case an exception was thrown within the body of F()
. Is this possible to do cleanly? I have read about std::uncaught_exception
, but its use does not seem to be recommended.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
大多数人都使用
std::uncaught_exception()
来尝试判断异常是否处于待处理状态,因此如果尚不存在异常,他们可以从析构函数中抛出异常。这通常被认为不是一个好主意。如果您不想在抛出异常时撤消操作,那么它应该可以解决问题。
请记住,析构函数是释放对象拥有的任何资源的最后机会,因为析构函数结束后该对象不存在,并且它所持有的任何资源现在都将永久泄漏。如果 OpDo() 分配任何内存或文件句柄或其他任何内容,则无论如何您都需要在析构函数中处理它。
Most people have used
std::uncaught_exception()
to try to tell if an exception is pending, so they can throw an exception from a destructor if there isn't one already. That is generally considered Not A Good Idea.If you want to not undo an operation if an exception has thrown, it should do the trick.
Remember that the destructor is your last chance to release any resources an object has, because after the destructor ends the object does not exist, and any resources it held are now permanently leaked. If
OpDo()
allocates any memory or file handles or whatever, you do need to deal with that in the destructor no matter what.您可以颠覆Scope Guard 习惯用法。我们不是在没有抛出异常时不在析构函数中执行某些操作,而是相反,并且仅在没有抛出异常时执行某些操作:
当抛出异常时,
do_undo.no_exception() 永远不会被调用,因此永远不会将
noexcept_
值设置为 true。 :) 可以在 Ideone 上找到示例。You can subvert the Scope Guard idiom. Instead of not doing something in the destructor when no exception is thrown, we invert that and only do something if no exception is thrown:
When an exception is thrown,
do_undo.no_exception()
will never be called and thus never set thenoexcept_
value to true. :) An example can be found here on Ideone.假设您的 F 返回某个类 Helper:
当流程正常时 - 创建 Helper。引发异常时,不会创建 Helper 的副本。尝试通过放置到 Helper 的私有区域构造函数并将 F 声明为友元来使用此语义 - 这样就没有人可以创建 helper。
Let's assume that your F returns some class Helper:
When flow is normal - Helper is created. When exception is raised no copy of Helper is created. Try use this semantic by placing to private region constructor of Helper and declaring F as friend - so no one could create helper.