[PATCH] D156032: Implement CWG2137 (list-initialization from objects of the same type)

Mital Ashok via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 22 12:32:27 PDT 2023


MitalAshok created this revision.
Herald added a project: All.
MitalAshok updated this revision to Diff 543217.
MitalAshok added a comment.
MitalAshok edited the summary of this revision.
MitalAshok added a reviewer: rsmith.
MitalAshok published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

clang-format



================
Comment at: clang/lib/Sema/SemaInit.cpp:4249-4250
           InitializedEntity::EK_LambdaToBlockConversionBlockElement &&
-      UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isPRValue() &&
-      S.Context.hasSameUnqualifiedType(UnwrappedArgs[0]->getType(), DestType)) {
+      Args.size() == 1 && Args[0]->isPRValue() &&
+      S.Context.hasSameUnqualifiedType(Args[0]->getType(), DestType)) {
     // Convert qualifications if necessary.
----------------
This change unfortunately exposes the still-open [[ https://wg21.link/CWG2311 | CWG2311 ]] but allows `T{ object_of_type_T }` to consider user declared constructors.

I am working on a separate fix for CWG2311 (Consider constructors as below, but then if the chosen constructor is not an initializer-list constructor, elide it).



================
Comment at: clang/lib/Sema/SemaOverload.cpp:1470-1480
+      // C++ [over.ics.list]p6, per DR2137:
+      //   If C is not an initializer-list constructor and the initializer list
+      //   has a single element of type cv U, where U is X or a class derived
+      //   from X, the implicit conversion sequence has Exact Match rank if U is
+      //   X, or Conversion rank if U is derived from X.
+      if (const auto *InitList = dyn_cast<InitListExpr>(From);
+          InitList && InitList->getNumInits() == 1 &&
----------------
This allows with `struct T { T(const T&); } t; void f(T);` for `f({ t })` is an exact match standard conversion instead of a user-defined conversion


CWG2137 <https://wg21.link/CWG2137> summary: A non-aggregate class being initialized with an initializer list with a single element (previously changed by CWG1467 <https://wg21.link/CWG1467>) should prefer initializer-list constructors over copy constructors, like everywhere else.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156032

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/lib/Sema/SemaOverload.cpp
  clang/test/CXX/drs/dr14xx.cpp
  clang/test/CXX/drs/dr21xx.cpp
  clang/www/cxx_dr_status.html

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156032.543217.patch
Type: text/x-patch
Size: 7861 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230722/d2e5276d/attachment-0001.bin>


More information about the cfe-commits mailing list