Bug 20172 - Invalid non-type template parameters not diagnosed
Summary: Invalid non-type template parameters not diagnosed
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.3
: P2 normal
Target Milestone: 4.1.0
Assignee: Volker Reichelt
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: accepts-invalid, monitored, patch
Depends on:
Blocks:
 
Reported: 2005-02-23 16:38 UTC by Vladimir Marko
Modified: 2005-07-13 09:41 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-05-01 03:24:37


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vladimir Marko 2005-02-23 16:38:34 UTC
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
Comment 1 Volker Reichelt 2005-02-25 09:13:12 UTC
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.
Comment 2 Volker Reichelt 2005-06-10 09:58:08 UTC
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.
Comment 3 GCC Commits 2005-07-12 16:08:04 UTC
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

Comment 4 Volker Reichelt 2005-07-13 09:41:41 UTC
Fixed on mainline.