Skip to content

CWG motion 2: P2748R5 Disallow Binding a Returned Glvalue to a Temporary #6889

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

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions source/basic.tex
Original file line number Diff line number Diff line change
Expand Up @@ -4532,8 +4532,6 @@
persists until the completion of the full-expression
containing the \grammarterm{expression-list}.

\item The lifetime of a temporary bound to the returned value in a function \tcode{return} statement\iref{stmt.return} is not extended; the temporary is destroyed at the end of the full-expression in the \tcode{return} statement.

\item A temporary bound to a reference in a \grammarterm{new-initializer}\iref{expr.new} persists until the completion of the full-expression containing the \grammarterm{new-initializer}.
\begin{note}
This might introduce a dangling reference.
Expand Down
23 changes: 23 additions & 0 deletions source/statements.tex
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,29 @@
before the destruction of local variables\iref{stmt.jump} of the block
enclosing the \tcode{return} statement.

\pnum
In a function whose return type is a reference,
other than an invented function for \tcode{std::is_convertible}\iref{meta.rel},
a \tcode{return} statement that binds the returned reference to
a temporary expression\iref{class.temporary} is ill-formed.
\begin{example}
\begin{codeblock}
auto&& f1() {
return 42; // ill-formed
}
const double& f2() {
static int x = 42;
return x; // ill-formed
}
auto&& id(auto&& r) {
return static_cast<decltype(r)&&>(r);
}
auto&& f3() {
return id(42); // OK, but probably a bug
}
\end{codeblock}
\end{example}

\rSec2[stmt.return.coroutine]{The \keyword{co_return} statement}%
\indextext{\idxcode{co_return}}%
\indextext{coroutine return|see{\tcode{co_return}}}%
Expand Down