diff mbox

C++0x PATCH for c++/46129 (ICE with default argument in member class of template)

Message ID 4CC1D80B.1050406@redhat.com
State New
Headers show

Commit Message

Jason Merrill Oct. 22, 2010, 6:29 p.m. UTC
The testcase in this PR was causing an ICE because we were running 
tsubst on a default argument that had already been tsubsted.  The bug 
turned out to be that the initial tsubst shouldn't have happened; 
default arguments in a template should not be instantiated until they 
are used.

Tested x86_64-pc-linux-gnu, applied to trunk.

Comments

Paolo Carlini Oct. 22, 2010, 8:19 p.m. UTC | #1
On 10/22/2010 08:29 PM, Jason Merrill wrote:
> The testcase in this PR was causing an ICE because we were running
> tsubst on a default argument that had already been tsubsted.  The bug
> turned out to be that the initial tsubst shouldn't have happened;
> default arguments in a template should not be instantiated until they
> are used.
Crazy, I was pretty sure we were already doing the right thing about this...

Paolo.
diff mbox

Patch

commit 70699d1c4404551084003c2836e2ee0708987f92
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Oct 22 10:23:20 2010 -0400

    	PR c++/46129
    	* pt.c (instantiate_class_template): Don't instantiate default
    	arguments.

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 85a5ea5..19e8512 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -8238,17 +8238,12 @@  instantiate_class_template (tree type)
   finish_struct_1 (type);
   TYPE_BEING_DEFINED (type) = 0;
 
-  /* Now that the class is complete, instantiate default arguments for
-     any member functions.  We don't do this earlier because the
-     default arguments may reference members of the class.  */
-  if (!PRIMARY_TEMPLATE_P (templ))
-    for (t = TYPE_METHODS (type); t; t = DECL_CHAIN (t))
-      if (TREE_CODE (t) == FUNCTION_DECL
-	  /* Implicitly generated member functions will not have template
-	     information; they are not instantiations, but instead are
-	     created "fresh" for each instantiation.  */
-	  && DECL_TEMPLATE_INFO (t))
-	tsubst_default_arguments (t);
+  /* We don't instantiate default arguments for member functions.  14.7.1:
+
+     The implicit instantiation of a class template specialization causes
+     the implicit instantiation of the declarations, but not of the
+     definitions or default arguments, of the class member functions,
+     member classes, static data members and member templates....  */
 
   /* Some typedefs referenced from within the template code need to be access
      checked at template instantiation time, i.e now. These types were
diff --git a/gcc/testsuite/g++.dg/template/defarg14.C b/gcc/testsuite/g++.dg/template/defarg14.C
new file mode 100644
index 0000000..1fe87e3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/defarg14.C
@@ -0,0 +1,13 @@ 
+// PR c++/46129
+// The default argument for A<int>::B::operator() should not be instantiated
+
+template <class T>
+struct A {
+  struct B {
+    void operator () (const T& d_ = f(T()) ) { }
+  };
+};
+
+int main() {
+  A<int>::B b;
+}