Does computed `goto` respect C++ object lifetime?

杀马特。学长 韩版系。学妹 提交于 2020-04-11 02:02:29

问题


Regular goto in C++ respects object lifetime - that is using goto to jump out of a block will run the destructors for the appropriate local variables.

start:
NonTrivial object;
if (again()) goto start;  // will call object.~NonTrivial()

Is the same true when using the Labels as Values extension?

start:
NonTrivial object;
goto *(again() ? &&start : &&end);
end:

GCC happily accepts this code, but seems to ignore any effect goto might have on object's lifetime. clang complains. Look for calls to NonTrivial() and ~NonTrivial() in Compiler Explorer.

Which compiler behaves correctly? Is it even possible, in general, to support this kind of indirect branching and also correctly manage object lifetime and RAII?

来源:https://stackoverflow.com/questions/60812880/does-computed-goto-respect-c-object-lifetime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!