This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 118b. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-09-28


3052. Unclear handling of checks on discarded return statements

Section: 8.8.4  [stmt.return]     Status: open     Submitter: Richard Smith     Date: 2025-08-02

(From submission #737.)

Consider:

  auto f() {
    if constexpr (false) return;
    return 3;
  }

Is this well-formed? The first return statement is discarded, so the return type is not deduced from it. However, 8.8.4 [stmt.return] paragraph 2 still seems to apply; discarded return statements are not excluded:

... A return statement with no operand shall be used only in a function whose return type is cv void, a constructor (11.4.5 [class.ctor]), or a destructor (11.4.7 [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; ...

Suggested resolution:

Change in 8.8.4 [stmt.return] paragraph 2 as follows:

... A return statement with no operand shall be used appear as a non-discarded statement (8.5.2 [stmt.if]) only in a function whose return type is cv void, a constructor (11.4.5 [class.ctor]), or a destructor (11.4.7 [class.dtor]). A return statement with an operand of type void shall be used appear as a non-discarded statement only in a function that has a cv void return type. A return statement with any other operand shall be used appear as a non-discarded statement only in a function that has a return type other than cv void; such a non-discarded return statement initializes the returned reference or prvalue result object of the (explicit or implicit) function call by copy-initialization (9.5 [dcl.init]) from the operand.