Files
cppdraft_translate/cppdraft/stmt/jump.md
2025-10-25 03:02:53 +03:00

13 KiB
Raw Blame History

[stmt.jump]

8 Statements [stmt]

8.8 Jump statements [stmt.jump]

8.8.1 General [stmt.jump.general]

1

#

Jump statements unconditionally transfer control.

jump-statement:
break ;
continue ;
return expr-or-braced-init-listopt ;
coroutine-return-statement
goto identifier ;

2

#

[Note 1:

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 ([stmt.dcl]).

For temporaries, see [class.temporary].

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 2:

A suspension of a coroutine ([expr.await]) is not considered to be an exit from a scope.

— end note]

8.8.2 The break statement [stmt.break]

1

#

A break statement shall be enclosed by ([stmt.pre])an iteration-statement ([stmt.iter]), an expansion-statement ([stmt.expand]), or a switch statement ([stmt.switch]).

The break statement causes termination of the innermost such enclosing statement; control passes to the statement following the terminated statement, if any.

8.8.3 The continue statement [stmt.cont]

1

#

A continue statement shall be enclosed by ([stmt.pre])an iteration-statement or an expansion-statement.

If the innermost enclosing such statement X is an iteration-statement ([stmt.iter]), the continue statement causes control to pass to the end of the statement or compound-statement of X.

Otherwise, control passes to the end of the compound-statement of the current Si ([stmt.expand]).

8.8.4 The return statement [stmt.return]

1

#

A function returns control to its caller by the return statement.

2

#

The expr-or-braced-init-list of a return statement is called its operand.

A return statement with no operand shall be used only in a function whose return type iscv void, a constructor ([class.ctor]), or a destructor ([class.dtor]).

A return statement with an operand of type void shall be used only in a function that has a cv void return type.

A return statement with any other operand shall be used only in a function that has a return type other than cv void;the return statement initializes the returned reference or prvalue result object of the (explicit or implicit) function call by copy-initialization from the operand.

[Note 1:

A constructor or destructor does not have a return type.

— end note]

[Note 2:

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 can be elided or converted to a move operation if an automatic storage duration variable is returned ([class.copy.elision]).

— end note]

3

#

The destructor for the result object is potentially invoked ([class.dtor], [except.ctor]).

[Example 1: class A {~A() {}}; A f() { return A(); } // error: destructor of A is private (even though it is never invoked) — end example]

4

#

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 that is neither main ([basic.start.main]) nor a coroutine ([dcl.fct.def.coroutine]) results in undefined behavior.

5

#

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.

[Note 3:

These operations are sequenced before the destruction of local variables in each remaining enclosing block of the function ([stmt.dcl]), which, in turn, is sequenced before the evaluation of postcondition assertions of the function ([dcl.contract.func]), which, in turn, is sequenced before the destruction of function parameters ([expr.call]).

— end note]

6

#

In a function whose return type is a reference, other than an invented function for std::is_convertible ([meta.rel]), a return statement that binds the returned reference to a temporary expression ([class.temporary]) is ill-formed.

[Example 2: auto&& f1() {return 42; // ill-formed}const double& f2() {static int x = 42; return x; // ill-formed}auto&& id(auto&& r) {return static_cast<decltype(r)&&>(r);}auto&& f3() {return id(42); // OK, but probably a bug} — end example]

8.8.5 The co_return statement [stmt.return.coroutine]

coroutine-return-statement:
co_return expr-or-braced-init-listopt ;

1

#

A co_return statement transfers control to the caller or resumer of a coroutine ([dcl.fct.def.coroutine]).

A coroutine shall not enclose a return statement ([stmt.return]).

[Note 1:

For this determination, it is irrelevant whether the return statement is enclosed by a discarded statement ([stmt.if]).

— end note]

2

#

The expr-or-braced-init-list of a co_return statement is called its operand.

Let p be an lvalue naming the coroutine promise object ([dcl.fct.def.coroutine]).

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:

3

#

If a search for the name return_void in the scope of the promise type finds any declarations, flowing off the end of a coroutine's function-body is equivalent to a co_return with no operand; otherwise flowing off the end of a coroutine's function-body results in undefined behavior.

8.8.6 The goto statement [stmt.goto]

1

#

The goto statement unconditionally transfers control to the statement labeled by the identifier.

The identifier shall be alabel located in the current function.