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

[class.copy.elision] No implicit moves for reference return types #4842

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jensmaurer
Copy link
Member

Fixes #4839

source/classes.tex Outdated Show resolved Hide resolved
@@ -6184,8 +6184,10 @@
In the following copy-initialization contexts,
a move operation is first considered before attempting a copy operation:
\begin{itemize}
\item If the \grammarterm{expression} in a \tcode{return}\iref{stmt.return} or
\tcode{co_return}\iref{stmt.return.coroutine} statement
\item If the \grammarterm{expression}
Copy link
Contributor

@xmh0511 xmh0511 Aug 27, 2021

Choose a reason for hiding this comment

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

It seems we also should give that restriction to lambda-expression

[]()->A&{
  /*....*/
};

In this example, the return type is a reference type. Anyway, I think we could uniformly restrict the return type to be an object type in this bullet. Since the return type of a coroutine is always an object type as per [coroutine.traits.primary#1]. In both the function that is non-coroutine and a lambda-expression, they all should be restricted for their return type.

If the expression in a return ([stmt.return]) or co_­return ([stmt.return.coroutine]) statement is a (possibly parenthesized) id-expression that names an implicitly movable entity declared in the body or parameter-declaration-clause of the innermost enclosing function or lambda-expression whose return type shall be (possibly cv-qualified) class type.

If the first overload resolution fails or was not performed

It seems the overload resolution always performs even though the candidate set is empty.

Copy link
Member Author

Choose a reason for hiding this comment

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

The first overload resolution is not performed if we don't have an implicitly movable entity to start with.

Copy link
Contributor

Choose a reason for hiding this comment

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

According to the whole rule, it is based on we do have an implicitly movable entity. Since it says as if the expression or operand were an rvalue. Since we have such a expression or operand, the overload resolution definitely performs.

Copy link
Member Author

Choose a reason for hiding this comment

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

No.

p1 talks about copy/move elision, which is totally different.

p3 talks about trying a move under certain circumstances; p3 starts with the definition of implicitly movable entity. Then, it says "In the following situations, a move is considered before attempting a copy". If e.g. the "return" is returning a global variable, this is not one of those situations, and we never try a move. We always try a copy, though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Reworded to cover both functions and lambda-expressions.

Copy link
Contributor

@xmh0511 xmh0511 Aug 30, 2021

Choose a reason for hiding this comment

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

If we phrase it as the following, it seems more clear

if the expression or operand is an implicitly movable entity, overload resolution to select the constructor for the copy or the return_­value overload to call is first performed as if the expression or operand were an rvalue. Otherwise, or if the overload resolution fails, overload resolution is performed again, considering the expression or operand as an lvalue.

@xmh0511
Copy link
Contributor

xmh0511 commented Aug 28, 2021

At least, the lambda-expression in this bullet should also have non-reference return type.

@tkoeppe tkoeppe added the cwg Issue must be reviewed by CWG. label Sep 25, 2021
@wg21bot wg21bot added the needs rebase The pull request needs a git rebase to resolve merge conflicts. label Oct 24, 2021
@jensmaurer jensmaurer removed the needs rebase The pull request needs a git rebase to resolve merge conflicts. label Oct 25, 2021
@wg21bot wg21bot added the needs rebase The pull request needs a git rebase to resolve merge conflicts. label Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cwg Issue must be reviewed by CWG. needs rebase The pull request needs a git rebase to resolve merge conflicts.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[class.copy.elision] p3 The return type in the first bullet should be restricted
5 participants