Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stmt.return,class.{ctor,dtor}] Clarify no return operand #4737

Merged
merged 1 commit into from Jan 14, 2022

Conversation

jensmaurer
Copy link
Member

Highlight that constructors and destructors do not have a
return type and thus a return statement within a constructor
or destructor cannot have an operand.

Supersedes #3647

\indextext{conversion!return type}%
the \tcode{return} statement initializes the
glvalue result or prvalue result object of the (explicit or implicit) function call
by copy-initialization\iref{dcl.init} from the operand.
\begin{note}
A constructor or destructor does not have a return type.
Copy link
Contributor

@xmh0511 xmh0511 Aug 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we claim that a constructor of a class does not have a return type, consider [dcl.init.general] p15

if the function is a constructor, the call is a prvalue of the cv-unqualified version of the destination type whose result object is initialized by the constructor.

[expr.call] p14

A function call is an lvalue if the result type is an lvalue reference type or an rvalue reference to function type, an xvalue if the result type is an rvalue reference to object type, and a prvalue otherwise.

These rules will be in conflict. How could the call of a constructor be a prvalue of the cv-unqualified version of the destination type?

Similarly, if a destructor does not have a return type, how does the following rule work?

If the postfix-expression names a destructor or pseudo-destructor ([expr.prim.id.dtor]), the type of the function call expression is void;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing the quoted text in [dcl.init.general] p15.

A constructor invocation is not a syntactic function call as described in [expr.call] (it's an explicit type conversion instead), thus any statements about the type of the function call expression don't apply.

We can explicitly call a destructor in a function call expression, and the rule you're quoting overrides the general rule that the type of a function call expression is the return type of the called function.

I'm not seeing any conflicts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing the quoted text in [dcl.init.general] p15.

It is moved to dcl.init#general-16.6.3 in the current draft

if the function is a constructor, the call is a prvalue of the cv-unqualified version of the destination type whose result object is initialized by the constructor. The call is used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jensmaurer Final thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constructors / destructors don't have a return type in the "function declaration" sense, and that absence makes "return something" syntactically ill-formed. I think the quoted stuff doesn't change that; it just means that a constructor call (in the context of copy-initialization, not in general) has (is considered to have) a certain type and value category. That has no bearing on the syntactic constraints on constructor declarations.

I thinks this should go in.

source/classes.tex Outdated Show resolved Hide resolved
@@ -2078,8 +2079,13 @@
the selected destructor may be deleted\iref{dcl.fct.def.delete}.

\pnum
\indextext{restriction!destructor}%
\indextext{constructor!address of}%
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

destructor

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks -- could you rebase please? Something weird seems to have happened to this branch.

Highlight that constructors and destructors do not have a
return type and thus a return statement within a constructor
or destructor cannot have an operand.
@jensmaurer
Copy link
Member Author

Rebased and force-pushed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants