如何检测析构函数中的堆栈展开

发布于 2024-09-28 20:09:09 字数 240 浏览 5 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆梦 2024-10-05 20:09:10

大多数人都使用 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.

酒儿 2024-10-05 20:09:10

您可以颠覆Scope Guard 习惯用法。我们不是在没有抛出异常时在析构函数中执行某些操作,而是相反,并且在没有抛出异常时执行某些操作:

class DoUndoRAII{
public:
  DoUndoRAII()
    : noexcept_(false)
  {
    // your stuff here
  }

  ~DoUndoRAII(){
    if(noexcept_){
      // do whatever you need to do
    }
  }

  void no_exception(){
    noexcept_ = true;
  }

private:
  bool noexcept_;
};

void func(){
  DoUndoRAII do_undo;

  // last line
  do_undo.no_exception();
}

当抛出异常时,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:

class DoUndoRAII{
public:
  DoUndoRAII()
    : noexcept_(false)
  {
    // your stuff here
  }

  ~DoUndoRAII(){
    if(noexcept_){
      // do whatever you need to do
    }
  }

  void no_exception(){
    noexcept_ = true;
  }

private:
  bool noexcept_;
};

void func(){
  DoUndoRAII do_undo;

  // last line
  do_undo.no_exception();
}

When an exception is thrown, do_undo.no_exception() will never be called and thus never set the noexcept_ value to true. :) An example can be found here on Ideone.

感情旳空白 2024-10-05 20:09:10

假设您的 F 返回某个类 Helper:

Helper F()
{
     MyClass doUndoWrapper;
}

当流程正常时 - 创建 Helper。引发异常时,不会创建 Helper 的副本。尝试通过放置到 Helper 的私有区域构造函数并将 F 声明为友元来使用此语义 - 这样就没有人可以创建 helper。

class Helper
{
private:
    friend Helper F();
    Helper(){ //place there OpDo semantic - first entry 
              // construct this class
    Helper(const Helper& copy){ //this must present to allow stack operations
              // copy constructor will be called at end of `F` to return value
              // so place OpUndo semantic there to mark success without exception

Let's assume that your F returns some class Helper:

Helper F()
{
     MyClass doUndoWrapper;
}

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.

class Helper
{
private:
    friend Helper F();
    Helper(){ //place there OpDo semantic - first entry 
              // construct this class
    Helper(const Helper& copy){ //this must present to allow stack operations
              // copy constructor will be called at end of `F` to return value
              // so place OpUndo semantic there to mark success without exception
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文