goto和destructors是否兼容? [英] Are goto and destructors compatible?

查看:127
本文介绍了goto和destructors是否兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码导致未定义的行为:

This code leads to undefined behavior:


void some_func() {
  goto undefined;
  {
    T x = T();
    undefined:
  }
}

但是这段代码怎么办?会调用x的析构函数吗?我认为会是,但我想确定。 :)

But what about this code? Will the destructor of x be called? I think it will be, but I want to be sure. :)


void some_func() {
  {
    T x = T();
    goto out;
  }
  out:
}


推荐答案

是的,析构函数将按预期被调用,就像你早期退出范围一样。

Yes, destructors will be called as expected, the same as if you exited the scope early due to an exception.

标准6.6 / ):


在从范围退出时(无论如何完成),调用具有在该范围内声明的自动存储持续时间的所有构造对象的析构函数,以它们的声明的相反顺序。

On exit from scope (however accomplished), destructors are called for all constructed objects with automatic storage duration that are declared in that scope, in the reverse order of their declaration.

这篇关于goto和destructors是否兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆