/* [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",
--- /dev/null
+// 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>());
+}