cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
if (TREE_CODE (type) == REFERENCE_TYPE
&& TYPE_REF_IS_RVALUE (type)
- && lvalue_or_rvalue_with_address_p (expr)
+ && real_lvalue_p (expr)
&& reference_related_p (TREE_TYPE (type), intype)
&& (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
{
- expr = build_typed_address (expr, type);
- return convert_from_reference (expr);
+ /* Handle the lvalue case here by casting to lvalue reference and
+ then changing it to an rvalue reference. Casting an xvalue to
+ rvalue reference will be handled by the main code path. */
+ tree lref = cp_build_reference_type (TREE_TYPE (type), false);
+ result = (perform_direct_initialization_if_possible
+ (lref, expr, c_cast_p, complain));
+ result = cp_fold_convert (type, result);
+ /* Make sure we don't fold back down to a named rvalue reference,
+ because that would be an lvalue. */
+ if (DECL_P (result))
+ result = build1 (NON_LVALUE_EXPR, type, result);
+ return convert_from_reference (result);
}
/* Resolve overloaded address here rather than once in
--- /dev/null
+// PR c++/51406
+// { dg-do run { target c++11 } }
+
+extern "C" int printf(const char *,...);
+extern "C" void abort();
+
+struct A { int a; A() : a(1) {} };
+struct B { int b; B() : b(2) {} };
+struct X : A, B {};
+
+int main() {
+ X x;
+ int a=static_cast<A&&>(x).a;
+ int b=static_cast<B&&>(x).b;
+ // printf ("%d %d\n", a, b);
+ if (a!=1 || b!=2) abort();
+}