[PATCH] D26231: [Sema] Allow static_cast<T&&>(e) to check explicit conversions for non-reference-related types.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 2 16:06:47 PDT 2016

rsmith accepted this revision.
rsmith added inline comments.
This revision is now accepted and ready to land.

Comment at: include/clang/Basic/DiagnosticSemaKinds.td:5946
+def err_bad_rvalue_to_rvalue_cast : Error<
+  "cannot cast from rvalue reference of type %1 to rvalue reference type %2; types are "
+  "not compatible">;
Delete the first "reference" here. We're never casting from a reference (there are no expressions of reference type).

Comment at: lib/Sema/SemaCast.cpp:1148
+  // FIXME C++1z doesn't allow this conversions for xvalues.
   if (!SrcExpr->isGLValue())
Actually, it does, but it's covered by p4. (p3 now only covers the special lvalue->xvalue case for `static_cast`, which is not permitted by p4.) It's fine for us to still handle this here, or to allow the p4 case to deal with it instead. It's probably better to keep handling it here, since we produce better diagnostics this way.

Comment at: lib/Sema/SemaCast.cpp:1171-1173
+    msg = SrcExpr->isLValue() ? diag::err_bad_lvalue_to_rvalue_cast
+                              : diag::err_bad_rvalue_to_rvalue_cast;
     return TC_Failed;
OK, so the idea here is that p4 will never apply in a case where the reference is reference-related to the source but not reference-compatible, and no other rules would let us static_cast to reference type? It would be good to add a comment explaining that, since it's not immediately obvious.

Comment at: test/CXX/expr/expr.post/expr.static.cast/p3-p4-0x.cpp:29
+  A &&ar6 = static_cast<A&&>(xvalue<D>());
+  A &&ar7 = static_cast<A&&>(prvalue<D>());
It'd be good to also have some negative tests here, for cases like 
A &&ar8 = static_cast<A&&>(prvalue<const A>());


More information about the cfe-commits mailing list