From 065cec90eef1aba745b12e0cd9d8e08256e5b198 Mon Sep 17 00:00:00 2001 From: Mohit Mishra Date: Tue, 17 Nov 2026 09:38:14 +0530 Subject: [PATCH] Make a braced cast to an rvalue reference that binds to a temporary an xvalue. [GH #507] --- src/overload.c | 6 +++ .../gh-507-braced-cast-rvalue-ref.txt | 18 +++++++ .../tests/changes/.GH507.rto/default.1.1.txt | 1 + .../tests/changes/.GH507.rto/default.2.1.txt | 1 + tests/tests/changes/GH507.sft.cpp | 54 +++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 src/pending_changes/gh-507-braced-cast-rvalue-ref.txt create mode 100644 tests/tests/changes/.GH507.rto/default.1.1.txt create mode 100644 tests/tests/changes/.GH507.rto/default.2.1.txt create mode 100644 tests/tests/changes/GH507.sft.cpp diff --git a/src/overload.c b/src/overload.c index 659e86441d..384431750d 100644 --- a/src/overload.c +++ b/src/overload.c @@ -28264,6 +28264,12 @@ will be an lvalue instead of the usual prvalue. leave_as_object, conv_context, ec_bad_initializer_type); + if (is_cast && is_rvalue_reference_type(dest_type) && + is_expression_operand(&operand)) { + /* The temporary comes back as an lvalue, but a cast to an + rvalue reference type, like T{x}, produces an xvalue. */ + conv_rvalue_reference_result_to_xvalue(&operand); + } /* if */ } /* if */ braced_init = FALSE; } else if (list == NULL) { diff --git a/src/pending_changes/gh-507-braced-cast-rvalue-ref.txt b/src/pending_changes/gh-507-braced-cast-rvalue-ref.txt new file mode 100644 index 0000000000..c935b68b7d --- /dev/null +++ b/src/pending_changes/gh-507-braced-cast-rvalue-ref.txt @@ -0,0 +1,18 @@ +[GH #507] +Braced cast to an rvalue reference type that binds to a temporary + +A braced functional-notation cast to an rvalue reference type produced an +lvalue instead of an xvalue when the reference was bound to a temporary, +i.e., when the element is not reference-related to the referenced type. +This includes conversion functions, standard conversions, an empty list, +and a list for an array. For example: + + struct NE { operator int(); } ne; + using R = int&&; + using RA = int(&&)[2]; + int &&a = R{ne}; // Error: an rvalue reference cannot be bound + // to an lvalue. + int &&b = R{'a'}; // Same error. + int (&&c)[2] = RA{1, 2}; // Same error. + +The result of such a cast is now an xvalue. diff --git a/tests/tests/changes/.GH507.rto/default.1.1.txt b/tests/tests/changes/.GH507.rto/default.1.1.txt new file mode 100644 index 0000000000..6cbd97ece6 --- /dev/null +++ b/tests/tests/changes/.GH507.rto/default.1.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=1 --c++11 Test_name.c diff --git a/tests/tests/changes/.GH507.rto/default.2.1.txt b/tests/tests/changes/.GH507.rto/default.2.1.txt new file mode 100644 index 0000000000..d7c18a04c5 --- /dev/null +++ b/tests/tests/changes/.GH507.rto/default.2.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=2 --c++17 Test_name.c diff --git a/tests/tests/changes/GH507.sft.cpp b/tests/tests/changes/GH507.sft.cpp new file mode 100644 index 0000000000..edd030f120 --- /dev/null +++ b/tests/tests/changes/GH507.sft.cpp @@ -0,0 +1,54 @@ +//type:fp +//options:--c++11:--c++17 +//remark:[GH #507] Braced cast to an rvalue reference that needs a temporary +// +// A braced functional cast to an rvalue reference type, like R{x} with +// "using R = int&&", produced an lvalue instead of an xvalue when the +// reference was bound to a temporary, i.e., when the element is not +// reference-related to the referenced type (a conversion function, a +// standard conversion, an empty list, or a list for an array). + +template struct is_lref { static const bool value = false; }; +template struct is_lref { static const bool value = true; }; +template struct is_rref { static const bool value = false; }; +template struct is_rref { static const bool value = true; }; +struct NE { operator int(); } ne; +struct NL { operator int&(); } nl; +using R = int&&; +using CR = const int&; +int &&a = R{ne}; +static_assert(!is_lref::value, ""); +static_assert(is_rref::value, ""); +static_assert(is_rref::value, ""); +static_assert(is_rref::value, ""); +static_assert(is_lref::value, ""); +static_assert(is_rref::value, ""); +struct S { S(); S(int); }; +struct CS { operator int(); } cs; +using RS = S&&; +S &&b = RS{cs}; +static_assert(is_rref::value, ""); +int &&c = R{nl}; +const int &d = CR{ne}; +void f(int&&); +void g() { f(R{ne}); f(R{nl}); } + +// Other cases where the reference is bound to a temporary. +using RL = long&&; +using CRR = const int&&; +using RA = int(&&)[2]; +char ch; +int i; +static_assert(is_rref::value, ""); +static_assert(is_rref::value, ""); +static_assert(is_rref::value, ""); +static_assert(is_rref::value, ""); +static_assert(is_rref::value, ""); +template void t(int n) { + static_assert(is_rref::value, ""); + static_assert(is_rref::value, ""); +} +template void t(int); +int &&e = R{'a'}; +long &&h = RL{i}; +int (&&k)[2] = RA{1, 2};