Skip to content

Commit fe11b0a

Browse files
authored
Merge 2024-03 CWG Motion 2
P2748R5 Disallow Binding a Returned Glvalue to a Temporary
2 parents 53a0238 + 9024fc9 commit fe11b0a

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

source/basic.tex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4532,8 +4532,6 @@
45324532
persists until the completion of the full-expression
45334533
containing the \grammarterm{expression-list}.
45344534

4535-
\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.
4536-
45374535
\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}.
45384536
\begin{note}
45394537
This might introduce a dangling reference.

source/statements.tex

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,29 @@
905905
before the destruction of local variables\iref{stmt.jump} of the block
906906
enclosing the \tcode{return} statement.
907907

908+
\pnum
909+
In a function whose return type is a reference,
910+
other than an invented function for \tcode{std::is_convertible}\iref{meta.rel},
911+
a \tcode{return} statement that binds the returned reference to
912+
a temporary expression\iref{class.temporary} is ill-formed.
913+
\begin{example}
914+
\begin{codeblock}
915+
auto&& f1() {
916+
return 42; // ill-formed
917+
}
918+
const double& f2() {
919+
static int x = 42;
920+
return x; // ill-formed
921+
}
922+
auto&& id(auto&& r) {
923+
return static_cast<decltype(r)&&>(r);
924+
}
925+
auto&& f3() {
926+
return id(42); // OK, but probably a bug
927+
}
928+
\end{codeblock}
929+
\end{example}
930+
908931
\rSec2[stmt.return.coroutine]{The \keyword{co_return} statement}%
909932
\indextext{\idxcode{co_return}}%
910933
\indextext{coroutine return|see{\tcode{co_return}}}%

0 commit comments

Comments
 (0)