Are goto and destructors compatible?

前端 未结 1 973
日久生厌
日久生厌 2020-12-30 04:05

This code leads to undefined behavior:


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

The constructor is not

相关标签:
1条回答
  • 2020-12-30 04:13

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

    Standard 6.6/2 (Jump statements):

    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.

    0 讨论(0)
提交回复
热议问题