Patchwork C++ PATCH for c++/55003 (wrong error with auto template static data member)

login
register
mail settings
Submitter Jason Merrill
Date Feb. 15, 2013, 1:17 a.m.
Message ID <511D8CB5.9040603@redhat.com>
Download mbox | patch
Permalink /patch/220578/
State New
Headers show

Comments

Jason Merrill - Feb. 15, 2013, 1:17 a.m.
Normally we defer instantiation of the initializer of a static data 
member of a template class until it is actually needed.  If we need it 
for deducing the type of the variable, we can go ahead and instantiate 
it at that point.

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

Patch

commit fe74d7110cbaf9ccf153e2950eee04fe0907a988
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Feb 14 12:39:19 2013 -0500

    	PR c++/55003
    	* decl.c (cp_finish_decl): Force instantiation of an
    	auto static data member.

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index eb6c490..3d63389 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -6111,6 +6111,15 @@  cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
       tree d_init;
       if (init == NULL_TREE)
 	{
+	  if (DECL_TEMPLATE_INSTANTIATION (decl)
+	      && !DECL_TEMPLATE_INSTANTIATED (decl))
+	    {
+	      /* init is null because we're deferring instantiating the
+		 initializer until we need it.  Well, we need it now.  */
+	      instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
+	      return;
+	    }
+
 	  error ("declaration of %q#D has no initializer", decl);
 	  TREE_TYPE (decl) = error_mark_node;
 	  return;
diff --git a/gcc/testsuite/g++.dg/cpp0x/auto37.C b/gcc/testsuite/g++.dg/cpp0x/auto37.C
new file mode 100644
index 0000000..f4b2904
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/auto37.C
@@ -0,0 +1,14 @@ 
+// PR c++/55003
+// { dg-do compile { target c++11 } }
+
+template<typename T>
+struct A {
+  static const auto t
+    = (typename T::type)42;
+};
+
+struct X {
+  typedef int type;
+};
+
+A<X> a;