Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/overload.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions src/pending_changes/gh-507-braced-cast-rvalue-ref.txt
Original file line number Diff line number Diff line change
@@ -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.
Comment thread
daveedvdv-nvidia marked this conversation as resolved.
Resolved
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.
1 change: 1 addition & 0 deletions tests/tests/changes/.GH507.rto/default.1.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=1 --c++11 Test_name.c
1 change: 1 addition & 0 deletions tests/tests/changes/.GH507.rto/default.2.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=2 --c++17 Test_name.c
54 changes: 54 additions & 0 deletions tests/tests/changes/GH507.sft.cpp
Original file line number Diff line number Diff line change
@@ -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<class T> struct is_lref { static const bool value = false; };
template<class T> struct is_lref<T&> { static const bool value = true; };
template<class T> struct is_rref { static const bool value = false; };
template<class T> struct is_rref<T&&> { 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<decltype((R{ne}))>::value, "");
static_assert(is_rref<decltype((R{ne}))>::value, "");
static_assert(is_rref<decltype((R(ne)))>::value, "");
static_assert(is_rref<decltype((R{1}))>::value, "");
static_assert(is_lref<decltype((CR{ne}))>::value, "");
static_assert(is_rref<decltype((R{nl}))>::value, "");
struct S { S(); S(int); };
struct CS { operator int(); } cs;
using RS = S&&;
S &&b = RS{cs};
static_assert(is_rref<decltype((RS{cs}))>::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<decltype((R{'a'}))>::value, "");
static_assert(is_rref<decltype((R{ch}))>::value, "");
static_assert(is_rref<decltype((RL{i}))>::value, "");
static_assert(is_rref<decltype((R{}))>::value, "");
static_assert(is_rref<decltype((CRR{ne}))>::value, "");
template<class T> void t(int n) {
static_assert(is_rref<decltype((T{n}))>::value, "");
static_assert(is_rref<decltype((T{ch}))>::value, "");
}
template void t<long&&>(int);
int &&e = R{'a'};
long &&h = RL{i};
int (&&k)[2] = RA{1, 2};
Loading