[PATCH] D38216: [C++17] Fix class template argument deduction for default constructors without an initializer

Nicolas Lesser via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 25 05:04:32 PDT 2017


Rakete1111 created this revision.
Rakete1111 added a project: clang.

As the title says, this makes following code compile:

  template<typename> struct Foo {};
  Foo() -> Foo<void>;
  
  Foo f; // ok


https://reviews.llvm.org/D38216

Files:
  lib/Sema/SemaDecl.cpp
  test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.class.deduct/p1.cpp
  test/Parser/cxx1z-class-template-argument-deduction.cpp


Index: test/Parser/cxx1z-class-template-argument-deduction.cpp
===================================================================
--- test/Parser/cxx1z-class-template-argument-deduction.cpp
+++ test/Parser/cxx1z-class-template-argument-deduction.cpp
@@ -52,7 +52,7 @@
 
   operator A(); // expected-error {{requires template arguments; argument deduction not allowed in conversion function type}}
 
-  static A x; // expected-error {{declaration of variable 'x' with deduced type 'A' requires an initializer}}
+  static A x;
   static constexpr A y = 0;
 };
 
@@ -114,7 +114,6 @@
     (void)A{n};
     (void)new A(n);
     (void)new A{n};
-    // FIXME: We should diagnose the lack of an initializer here.
     (void)new A;
   }
 }
@@ -127,7 +126,7 @@
 
   auto k() -> A; // expected-error{{requires template arguments}}
 
-  A a; // expected-error {{declaration of variable 'a' with deduced type 'A' requires an initializer}}
+  A a;
   A b = 0;
   const A c = 0;
   A (parens) = 0; // expected-error {{cannot use parentheses when declaring variable with deduced class template specialization type}}
Index: test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.class.deduct/p1.cpp
===================================================================
--- test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.class.deduct/p1.cpp
+++ test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.class.deduct/p1.cpp
@@ -5,8 +5,7 @@
 A(int) -> A<char>;
 
 static constexpr inline const volatile A a = {}; // ok, specifiers are permitted
-// FIXME: There isn't really a good reason to reject this.
-A b; // expected-error {{requires an initializer}}
+A b;
 A c [[]] {};
 
 A d = {}, e = {};
@@ -13,6 +12,6 @@
 A f(0), g{}; // expected-error {{template arguments deduced as 'A<char>' in declaration of 'f' and deduced as 'A<int>' in declaration of 'g'}}
 
 struct B {
-  static A a; // expected-error {{requires an initializer}}
+  static A a;
 };
-extern A x; // expected-error {{requires an initializer}}
+extern A x;
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -10091,7 +10091,8 @@
   assert(Deduced && "deduceVarTypeFromInitializer for non-deduced type");
 
   // C++11 [dcl.spec.auto]p3
-  if (!Init) {
+  // Except for class argument deduction.
+  if (!Init && !isa<DeducedTemplateSpecializationType>(Deduced)) {
     assert(VDecl && "no init for init capture deduction?");
     Diag(VDecl->getLocation(), diag::err_auto_var_requires_init)
       << VDecl->getDeclName() << Type;
@@ -10110,7 +10111,10 @@
     InitializationKind Kind = InitializationKind::CreateForInit(
         VDecl->getLocation(), DirectInit, Init);
     // FIXME: Initialization should not be taking a mutable list of inits. 
-    SmallVector<Expr*, 8> InitsCopy(DeduceInits.begin(), DeduceInits.end());
+    SmallVector<Expr*, 8> InitsCopy;
+    if (Init)
+      InitsCopy = {DeduceInits.begin(), DeduceInits.end()};
+
     return DeduceTemplateSpecializationFromInitializer(TSI, Entity, Kind,
                                                        InitsCopy);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38216.116471.patch
Type: text/x-patch
Size: 3112 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170925/d1ee6814/attachment.bin>


More information about the cfe-commits mailing list