What scope and object-lifetime rules constrain goto in C++, and when can it be justified instead of structured control flow?
Show answer and explanation
A goto target label must be in the same function. On a transfer out of a scope, already-constructed automatic objects that are no longer active are destroyed in reverse construction order. A jump into a region that would bypass non-vacuous initialization is ill-formed; the language prevents code from observing an object whose required initialization was skipped. Backward transfers can leave and later re-enter scopes, so repeated construction and destruction must remain understandable. Structured loops, early returns, helper functions, and RAII usually express intent more clearly. A local goto can still be defensible in generated code or a tightly bounded state machine or multi-level error exit after alternatives are demonstrably less clear, but it must not replace ownership or cleanup design and cannot jump between functions.
A goto target label must be in the same function. On a transfer out of a scope, already-constructed automatic objects that are no longer active are destroyed in reverse construction order. A jump into a region that would bypass non-vacuous initialization is ill-formed; the language prevents code from observing an object whose required initialization was skipped. Backward transfers can leave and later re-enter scopes, so repeated construction and destruction must remain understandable. Structured loops, early returns, helper functions, and RAII usually express intent more clearly. A local goto can still be defensible in generated code or a tightly bounded state machine or multi-level error exit after alternatives are demonstrably less clear, but it must not replace ownership or cleanup design and cannot jump between functions.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.