commit 93e0f8336fe098431665731a91c23647fcd2a29e
Author: Jason Merrill <jason@redhat.com>
Date: Mon May 23 13:21:52 2011 -0400
PR c++/49105
* typeck.c (build_const_cast_1): Handle rvalue references.
@@ -6241,14 +6241,29 @@ build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
/* [expr.const.cast]
- An lvalue of type T1 can be explicitly converted to an lvalue of
- type T2 using the cast const_cast<T2&> (where T1 and T2 are object
- types) if a pointer to T1 can be explicitly converted to the type
- pointer to T2 using a const_cast. */
+ For two object types T1 and T2, if a pointer to T1 can be explicitly
+ converted to the type "pointer to T2" using a const_cast, then the
+ following conversions can also be made:
+
+ -- an lvalue of type T1 can be explicitly converted to an lvalue of
+ type T2 using the cast const_cast<T2&>;
+
+ -- a glvalue of type T1 can be explicitly converted to an xvalue of
+ type T2 using the cast const_cast<T2&&>; and
+
+ -- if T1 is a class type, a prvalue of type T1 can be explicitly
+ converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
+
if (TREE_CODE (dst_type) == REFERENCE_TYPE)
{
reference_type = dst_type;
- if (! real_lvalue_p (expr))
+ if (!TYPE_REF_IS_RVALUE (dst_type)
+ ? real_lvalue_p (expr)
+ : (CLASS_TYPE_P (TREE_TYPE (dst_type))
+ ? lvalue_p (expr)
+ : lvalue_or_rvalue_with_address_p (expr)))
+ /* OK. */;
+ else
{
if (complain & tf_error)
error ("invalid const_cast of an rvalue of type %qT to type %qT",
new file mode 100644
@@ -0,0 +1,21 @@
+// Test for const_cast to reference (5.2.11/4).
+// { dg-options -std=c++0x }
+
+template <class T> T&& xval();
+template <class T> T& lval();
+template <class T> T prval();
+
+struct A { };
+
+int main()
+{
+ const_cast<int&>(lval<int>());
+ const_cast<int&>(xval<int>()); // { dg-error "" }
+ const_cast<int&>(prval<int>()); // { dg-error "" }
+ const_cast<int&&>(lval<int>());
+ const_cast<int&&>(xval<int>());
+ const_cast<int&&>(prval<int>()); // { dg-error "" }
+ const_cast<A&&>(lval<A>());
+ const_cast<A&&>(xval<A>());
+ const_cast<A&&>(prval<A>());
+}
commit 120577c5010612c5f7cf2828c165997b72990ddd
Author: Jason Merrill <jason@redhat.com>
Date: Mon May 23 13:31:50 2011 -0400
PR c++/49105
* typeck.c (cp_build_c_cast): Don't strip cv-quals when
converting to reference.
(build_static_cast_1): Update for glvalues.
@@ -5768,11 +5768,11 @@ build_static_cast_1 (tree type, tree expr, bool c_cast_p,
return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
}
- /* "An lvalue of type cv1 T1 can be cast to type rvalue reference to
+ /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
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)
- && real_lvalue_p (expr)
+ && lvalue_or_rvalue_with_address_p (expr)
&& reference_related_p (TREE_TYPE (type), intype)
&& (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
{
@@ -6448,7 +6448,7 @@ cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
if (!CLASS_TYPE_P (type))
type = TYPE_MAIN_VARIANT (type);
result_type = TREE_TYPE (result);
- if (!CLASS_TYPE_P (result_type))
+ if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
result_type = TYPE_MAIN_VARIANT (result_type);
/* If the type of RESULT does not match TYPE, perform a
const_cast to make it match. If the static_cast or
new file mode 100644
@@ -0,0 +1,10 @@
+// PR c++/49105
+// { dg-options -std=c++0x }
+
+template<class T, class = decltype(T{})>
+char f(int);
+
+template<class T>
+auto f(...) -> char(&)[2];
+
+static_assert(sizeof(f<const int&&>(0)) == 1, "Error"); // #