gcc accepts the following ill-formed code (tested with gcc3.4.2 (mingw) and various cygwin versions (from 3.3.1 to 3.4.3 and an experimental snapshot 4.0.0-20050130) ): template <typename T> struct foo{ template <T t> static void bar() { } }; template <typename T> foo<T> tester(T) { return foo<T>(); } int main(){ tester(1.2); return 0; } std:14.7.1/1 says ... 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_; ... Thus, the implicit instantiation of struct foo<double> causes the instantiation of declaration template <double t> void foo<double>::bar(); which is ill-formed and should be diagnosed. Remark 1: The signature of bar is unimportant, it just has to be a function. If one declares a nested "template <T t> struct S;" gcc correctly complains when instantiating foo<double>. Remark 2: Shorter testcase consists of a declaration of the template foo and an explicit instantiation of foo<double>, but the wording for implicit instantiation is much cleaner in this case so I decided to use it. Regards, Vladimir Marko
Confirmed. Accepted since at least gcc 2.95.3. Reduced testcase: ================================ template<typename T> struct A { template<T> void foo(); }; A<double> a; ================================ Even A<void> is accepted.
Actually gcc does instantiate the declarations. It just doesn't check whether the non-type template arguments are valid in this situation. Adjusting the summary accordingly. Patch posted.
Subject: Bug 20172 CVSROOT: /cvs/gcc Module name: gcc Changes by: nathan@gcc.gnu.org 2005-07-12 16:07:39 Modified files: gcc/cp : ChangeLog pt.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/g++.dg/template: nontype12.C Log message: cp: PR c++/20172 * pt.c (tsubst_template_parms): Check for invalid non-type parameters. testsuite: PR c++/20172 * g++.dg/template/nontype12.C : New test. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.4817&r2=1.4818 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/pt.c.diff?cvsroot=gcc&r1=1.1014&r2=1.1015 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5752&r2=1.5753 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/nontype12.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
Fixed on mainline.