Skip to content

CWG3052 [stmt.return] Unclear handling of checks on discarded return statements #737

Description

@zygoloid

Reference (section label): [stmt.return]

Issue description:

It's unclear whether this example is intended to be valid:

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

The first return statement is discarded, so we do not use it when deducing the return type. (Although one implementation does, and rejects for that reason; the remaining tested implementations accept.) However, [stmt.return]/2 still appears to apply:

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

Nothing seems to exclude discarded return statements from this check. Presumably they should be excluded.

Suggested resolution:

Change in [stmt.return]/2:

The expr-or-braced-init-list of a return statement is called its operand. A return statement with no operand shall be used appear as a non-discarded statement ([stmt.if]) only in a function whose return type is cv void, a constructor ([class.ctor]), or a destructor ([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; the such a non-discarded return statement initializes the returned reference or prvalue result object of the (explicit or implicit) function call by copy-initialization from the operand.

This change also clarifies that the semantic constraints for copy-initialization of the return object from the argument are not checked for a discarded return statement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions