From patchwork Fri Feb 15 01:17:41 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: C++ PATCH for c++/55003 (wrong error with auto template static data member) From: Jason Merrill X-Patchwork-Id: 220578 Message-Id: <511D8CB5.9040603@redhat.com> To: gcc-patches List Date: Thu, 14 Feb 2013 20:17:41 -0500 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 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 +struct A { + static const auto t + = (typename T::type)42; +}; + +struct X { + typedef int type; +}; + +A a;