[stmt.jump] (original) (raw)
8 Statements [stmt.stmt]
8.7 Jump statements [stmt.jump]
On exit from a scope (however accomplished), objects with automatic storage duration that have been constructed in that scope are destroyed in the reverse order of their construction.
Transfer out of a loop, out of a block, or back past an initialized variable with automatic storage duration involves the destruction of objects with automatic storage duration that are in scope at the point transferred from but not at the point transferred to.
(See [stmt.dcl] for transfers into blocks).
[ Note
:
However, the program can be terminated (by callingstd::exit() orstd::abort() ([support.start.term]), for example) without destroying objects with automatic storage duration.
— end note
]
[ Note
:
A suspension of a coroutine ([expr.await]) is not considered to be an exit from a scope.
— end note
]
8.7.1 The break statement [stmt.break]
The break statement shall occur only in aniteration-statement or a switch statement and causes termination of the smallest enclosing iteration-statement orswitch statement; control passes to the statement following the terminated statement, if any.
8.7.2 The continue statement [stmt.cont]
Thecontinuestatement shall occur only in aniteration-statementand causes control to pass to the loop-continuation portion of the smallest enclosing iteration-statement, that is, to the end of the loop.
More precisely, in each of the statements
while (foo) { {
} contin: ; }
do { {
} contin: ; } while (foo);
for (;;) { {
} contin: ; }
a continue not contained in an enclosed iteration statement is equivalent to goto contin.
8.7.3 The return statement [stmt.return]
A function returns to its caller by the return statement.
A return statement with no operand shall be used only in a function whose return type iscv void, a constructor, or adestructor.
A return statement with an operand of type void shall be used only in a function whose return type is cv void.
A return statement with any other operand shall be used only in a function whose return type is not cv void;the return statement initializes the glvalue result or prvalue result object of the (explicit or implicit) function call by copy-initialization from the operand.
[ Note
:
A return statement can involve an invocation of a constructor to perform a copy or move of the operand if it is not a prvalue or if its type differs from the return type of the function.
A copy operation associated with a return statement may be elided or converted to a move operation if an automatic storage duration variable is returned ([class.copy.elision]).
— end note
]
[ Example
:
std::pairstd::string,int f(const char* p, int x) { return {p,x}; }
— end example
]
[ Example
:
class A { ~A() {} }; A f() { return A(); }
— end example
]
Flowing off the end of a constructor, a destructor, or a non-coroutine function with a cv void return type is equivalent to a return with no operand.
Otherwise, flowing off the end of a function other than main or a coroutine ([dcl.fct.def.coroutine]) results in undefined behavior.
The copy-initialization of the result of the call is sequenced before the destruction of temporaries at the end of the full-expression established by the operand of the return statement, which, in turn, is sequenced before the destruction of local variables ([stmt.jump]) of the block enclosing the return statement.
8.7.4 The co_return statement [stmt.return.coroutine]
coroutine-return-statement: co_return expr-or-braced-init-list ;
A coroutine shall not enclose a return statement ([stmt.return]).
[ Note
:
For this determination, it is irrelevant whether the return statement is enclosed by a discarded statement ([stmt.if]).
— end note
]
A co_return statement is equivalent to:
{ S; goto final-suspend; }
where final-suspend is the exposition-only label defined in [dcl.fct.def.coroutine]and S is defined as follows:
- If the operand is a braced-init-list or an expression of non-void type,S is p.return_value(expr-or-braced-init-list). The expression S shall be a prvalue of type void.
- Otherwise,S is the compound-statement { expression ; p.return_void(); }. The expression p.return_void()shall be a prvalue of type void.
If p.return_void() is a valid expression, flowing off the end of a coroutine is equivalent to a co_return with no operand; otherwise flowing off the end of a coroutine results in undefined behavior.
8.7.5 The goto statement [stmt.goto]
The goto statement unconditionally transfers control to the statement labeled by the identifier.
The identifier shall be alabel located in the current function.