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.
commit fe74d7110cbaf9ccf153e2950eee04fe0907a988
Author: Jason Merrill <ja...@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;