问题
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