[stmt.return] (original) (raw)

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

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_]

[Example 1: std::pairstd::string,int\ f(const char* p, int x) { return {p,x};} — _end example_]

[Example 2: 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.