]> granicus.if.org Git - clang/commitdiff
Template argument deduction for std::initializer_list arguments from initializer...
authorSebastian Redl <sebastian.redl@getdesigned.at>
Tue, 17 Jan 2012 22:49:58 +0000 (22:49 +0000)
committerSebastian Redl <sebastian.redl@getdesigned.at>
Tue, 17 Jan 2012 22:49:58 +0000 (22:49 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148352 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDeclCXX.cpp
lib/Sema/SemaTemplateDeduction.cpp
test/SemaCXX/cxx0x-initializer-stdinitializerlist.cpp

index 85c292ff4cb0d246de9940a0c8cde71da6d71c15..13face95e95f6894c8860731fd78ed43c4d8c04b 100644 (file)
@@ -5777,19 +5777,29 @@ bool Sema::isStdInitializerList(QualType Ty, QualType *Element) {
   if (!StdNamespace) // If we haven't seen namespace std yet, this can't be it.
     return false;
 
-  const RecordType *RT = Ty->getAs<RecordType>();
-  if (!RT)
-    return false;
+  ClassTemplateDecl *Template = 0;
+  const TemplateArgument *Arguments = 0;
 
-  ClassTemplateSpecializationDecl *Specialization =
-      dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
-  if (!Specialization)
-    return false;
+  if (const RecordType *RT = Ty->getAs<RecordType>()) {
 
-  if (Specialization->getSpecializationKind() != TSK_ImplicitInstantiation)
-    return false;
+    ClassTemplateSpecializationDecl *Specialization =
+        dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
+    if (!Specialization)
+      return false;
 
-  ClassTemplateDecl *Template = Specialization->getSpecializedTemplate();
+    if (Specialization->getSpecializationKind() != TSK_ImplicitInstantiation)
+      return false;
+
+    Template = Specialization->getSpecializedTemplate();
+    Arguments = Specialization->getTemplateArgs().data();
+  } else if (const TemplateSpecializationType *TST =
+                 Ty->getAs<TemplateSpecializationType>()) {
+    Template = dyn_cast_or_null<ClassTemplateDecl>(
+        TST->getTemplateName().getAsTemplateDecl());
+    Arguments = TST->getArgs();
+  }
+  if (!Template)
+    return false;
 
   if (!StdInitializerList) {
     // Haven't recognized std::initializer_list yet, maybe this is it.
@@ -5814,9 +5824,8 @@ bool Sema::isStdInitializerList(QualType Ty, QualType *Element) {
     return false;
 
   // This is an instance of std::initializer_list. Find the argument type.
-  if (Element) {
-    *Element = Specialization->getTemplateArgs()[0].getAsType();
-  }
+  if (Element)
+    *Element = Arguments[0].getAsType();
   return true;
 }
 
index ab2c2a3e678d8f490a62363fe256303e99f1d946..738e596ef3806c7264d88d1ba5fd65a43309912d 100644 (file)
@@ -2971,6 +2971,28 @@ Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
       if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType))
         continue;
 
+      // If the argument is an initializer list ...
+      if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) {
+        // ... then the parameter is an undeduced context, unless the parameter
+        // type is (reference to cv) std::initializer_list<P'>, in which case
+        // deduction is done for each element of the initializer list, and the
+        // result is the deduced type if it's the same for all elements.
+        QualType X;
+        // Removing references was already done.
+        if (!isStdInitializerList(ParamType, &X))
+          continue;
+
+        for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) {
+          if (TemplateDeductionResult Result =
+                DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, X,
+                                                   ILE->getInit(i)->getType(),
+                                                   Info, Deduced, TDF))
+            return Result;
+        }
+        // Don't track the argument type, since an initializer list has none.
+        continue;
+      }
+
       // Keep track of the argument type and corresponding parameter index,
       // so we can check for compatibility between the deduced A and A.
       OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx-1, 
@@ -3042,17 +3064,35 @@ Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
         break;
       }
 
-      // Keep track of the argument type and corresponding argument index,
-      // so we can check for compatibility between the deduced A and A.
-      if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType))
-        OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx, 
-                                                   ArgType));
+      // As above, initializer lists need special handling.
+      if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) {
+        QualType X;
+        if (!isStdInitializerList(ParamType, &X)) {
+          ++ArgIdx;
+          break;
+        }
 
-      if (TemplateDeductionResult Result
-          = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams,
-                                               ParamType, ArgType, Info,
-                                               Deduced, TDF))
-        return Result;
+        for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) {
+          if (TemplateDeductionResult Result =
+                DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, X,
+                                                   ILE->getInit(i)->getType(),
+                                                   Info, Deduced, TDF))
+            return Result;
+        }
+      } else {
+
+        // Keep track of the argument type and corresponding argument index,
+        // so we can check for compatibility between the deduced A and A.
+        if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType))
+          OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx, 
+                                                     ArgType));
+
+        if (TemplateDeductionResult Result
+            = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams,
+                                                 ParamType, ArgType, Info,
+                                                 Deduced, TDF))
+          return Result;
+      }
 
       // Capture the deduced template arguments for each parameter pack expanded
       // by this pack expansion, add them to the list of arguments we've deduced
index 87e7e8462ba59cb8b3fc115b63f7aa1af1a0ff3e..a08a04806cae00a84d8c1782205c3c79749a965d 100644 (file)
@@ -32,6 +32,11 @@ namespace std {
   };
 }
 
+template <typename T, typename U>
+struct same_type { static const bool value = false; };
+template <typename T>
+struct same_type<T, T> { static const bool value = true; };
+
 struct one { char c[1]; };
 struct two { char c[2]; };
 
@@ -87,3 +92,20 @@ void overloaded_call() {
     // But here, user-defined is worst in both cases.
     ov2({1, 2, D()}); // expected-error {{ambiguous}}
 }
+
+template <typename T>
+T deduce(std::initializer_list<T>); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}
+template <typename T>
+T deduce_ref(const std::initializer_list<T>&); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}
+
+void argument_deduction() {
+  static_assert(same_type<decltype(deduce({1, 2, 3})), int>::value, "bad deduction");
+  static_assert(same_type<decltype(deduce({1.0, 2.0, 3.0})), double>::value, "bad deduction");
+
+  deduce({1, 2.0}); // expected-error {{no matching function}}
+
+  static_assert(same_type<decltype(deduce_ref({1, 2, 3})), int>::value, "bad deduction");
+  static_assert(same_type<decltype(deduce_ref({1.0, 2.0, 3.0})), double>::value, "bad deduction");
+
+  deduce_ref({1, 2.0}); // expected-error {{no matching function}}
+}