[cfe-commits] r122425 - in /cfe/trunk: lib/Sema/SemaTemplateDeduction.cpp test/CXX/temp/temp.decls/temp.variadic/replace.cpp

Douglas Gregor dgregor at apple.com
Wed Dec 22 10:55:49 PST 2010


Author: dgregor
Date: Wed Dec 22 12:55:49 2010
New Revision: 122425

URL: http://llvm.org/viewvc/llvm-project?rev=122425&view=rev
Log:
When performing template argument deduction where the argument is a
dependent template specialization type, the number of template
arguments need not match precisely. Rather than checking the number of
arguments eagerly (which does not consider argument packs), let the
deduction routine for template argument lists cope with too many/too
few arguments.

Added:
    cfe/trunk/test/CXX/temp/temp.decls/temp.variadic/replace.cpp   (with props)
Modified:
    cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp

Modified: cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp?rev=122425&r1=122424&r2=122425&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp Wed Dec 22 12:55:49 2010
@@ -81,7 +81,7 @@
                         const TemplateArgument &Param,
                         const TemplateArgument &Arg,
                         TemplateDeductionInfo &Info,
-                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
+                      llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
 
 static Sema::TemplateDeductionResult
 DeduceTemplateArguments(Sema &S,
@@ -89,7 +89,8 @@
                         const TemplateArgument *Params, unsigned NumParams,
                         const TemplateArgument *Args, unsigned NumArgs,
                         TemplateDeductionInfo &Info,
-                      llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
+                        llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
+                        bool NumberOfArgumentsMustMatch = true);
 
 /// \brief If the given expression is of a form that permits the deduction
 /// of a non-type template parameter, return the declaration of that
@@ -309,14 +310,13 @@
 
 
     // Perform template argument deduction on each template
-    // argument.
-    // FIXME: This "min" function isn't going to work in general. We should
-    // probably pass down a flag that allows too few/too many arguments.
-    unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs());
+    // argument. Ignore any missing/extra arguments, since they could be
+    // filled in by default arguments.
     return DeduceTemplateArguments(S, TemplateParams, 
-                                   Param->getArgs(), NumArgs, 
-                                   SpecArg->getArgs(), NumArgs,
-                                   Info, Deduced);
+                                   Param->getArgs(), Param->getNumArgs(), 
+                                   SpecArg->getArgs(), SpecArg->getNumArgs(),
+                                   Info, Deduced,
+                                   /*NumberOfArgumentsMustMatch=*/false);
   }
 
   // If the argument type is a class template specialization, we
@@ -953,7 +953,8 @@
                         const TemplateArgument *Params, unsigned NumParams,
                         const TemplateArgument *Args, unsigned NumArgs,
                         TemplateDeductionInfo &Info,
-                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
+                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
+                        bool NumberOfArgumentsMustMatch) {
   unsigned ArgIdx = 0, ParamIdx = 0;
   for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams); 
        ++ParamIdx) {
@@ -962,7 +963,8 @@
       
       // Check whether we have enough arguments.
       if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
-        return Sema::TDK_TooFewArguments;
+        return NumberOfArgumentsMustMatch? Sema::TDK_TooFewArguments 
+                                         : Sema::TDK_Success;
       
       // Perform deduction for this P/A pair.
       if (Sema::TemplateDeductionResult Result
@@ -985,7 +987,8 @@
   }
   
   // If there is an argument remaining, then we had too many arguments.
-  if (hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
+  if (NumberOfArgumentsMustMatch &&
+      hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
     return Sema::TDK_TooManyArguments;
   
   return Sema::TDK_Success;

Added: cfe/trunk/test/CXX/temp/temp.decls/temp.variadic/replace.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/temp/temp.decls/temp.variadic/replace.cpp?rev=122425&view=auto
==============================================================================
--- cfe/trunk/test/CXX/temp/temp.decls/temp.variadic/replace.cpp (added)
+++ cfe/trunk/test/CXX/temp/temp.decls/temp.variadic/replace.cpp Wed Dec 22 12:55:49 2010
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s
+
+template<typename T, typename U>
+struct is_same {
+  static const bool value = false;
+};
+
+template<typename T>
+struct is_same<T, T> {
+  static const bool value = true;
+};
+
+// Simple metafunction that replaces the template arguments of
+// template template parameters with 'int'.
+template<typename T>
+struct EverythingToInt;
+
+template<template<typename ...> class TT, typename T1, typename T2>
+struct EverythingToInt<TT<T1, T2> > {
+  typedef TT<int, int> type;
+};
+
+template<typename...> struct tuple { };
+
+int check0[is_same<EverythingToInt<tuple<double, float>>::type, 
+                   tuple<int, int>>::value? 1 : -1];

Propchange: cfe/trunk/test/CXX/temp/temp.decls/temp.variadic/replace.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/CXX/temp/temp.decls/temp.variadic/replace.cpp
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/CXX/temp/temp.decls/temp.variadic/replace.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list