[PATCH] D87565: [Sema] Improve const_cast conformance to N4261

Richard Smith - zygoloid via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 13 16:36:49 PDT 2020


rsmith requested changes to this revision.
rsmith added inline comments.
This revision now requires changes to proceed.


================
Comment at: clang/lib/Sema/SemaCast.cpp:1781-1782
     //       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&&>.
 
----------------
This is the bullet that causes class pvalues and non-class prvalues to be treated differently by `const_cast` to an rvalue reference type, and it still exists unchanged in the latest C++ draft. As far as I can tell, the change here is not in line with the wording for N4261 / CWG issue 330, which Clang does implement.

(Now, I think the change you're suggesting here makes a lot of sense, but we should get agreement in WG21 to fix the language rules first.)


================
Comment at: clang/lib/Sema/SemaCast.cpp:1797-1801
+      // C++17 [expr.const.cast]p3
+      // For two similar types T1 and T2, a prvalue of type T1 may be explicitly
+      // converted to the type T2 using a const_cast if, considering the
+      // cv-decompositions of both types, each P1i is the same as P2i for all i.
+      // The result of a const_cast refers to the original entity.
----------------
In (for example) `const_cast<int&&>(0);`, `T1` is `int`, and `T2` is "rvalue reference to `int`". `T1` and `T2` are not similar types, because they have different types `U` (for `T1`, `U` is `int`; for `T2`, `U` is "rvalue reference to `int`"). So this doesn't apply.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D87565/new/

https://reviews.llvm.org/D87565



More information about the cfe-commits mailing list