[C++ PATCH] [PR15947] Fix use of destructor as a template name

Giovanni Bajo giovannibajo@libero.it
Sun Jun 13 15:55:00 GMT 2004


Hello,

PR 15947 is about the use of a destructor as a template name:

--------------------------------------------------------------
template <class Type> class Test
{
public:
	Test(void);
	~Test(void);
};

template <class Type>
Test<Type>::Test<Type>(void) {}

template <class Type>
Test<Type>::~Test<Type>(void) {}
--------------------------------------------------------------

It can be noticed that the use of constructor as template
(Test<Type>::Test<Type> instead of Test<Type>::Test) is illegal and correctly
rejected by GCC on mainline (due to a recent patch of mine). On the other hand,
it is not clear from the standard if the use of destructor as a template name
is allowed. There are a couple of open DRs about the name lookup of destructor
(DR 399 and DR 244), but there seems to be agreement that this case should be
allowed.

Anyway, GCC 3.3 used to accept it. The new parser used to accept it as well,
but it started rejecting it as a side-effect of trying to detect missing
'template' keywords to disambiguate nested templates. This looks like a
regression to me, it is surely not what we wanted to achieve.

My patch gets back to the 3.3 behaviour (and, hopefully, to the behaviour
mandated by the standard). Since I was at it, I also changed a diagnostic line
containing a suggestion for the user from error() to inform(), which is better
suited for that context.

Tested on i686-pc-linux-gnu with no new regressions, OK for mainline? OK for
3.4.1 (or 3.4.2)?

Giovanni Bajo


cp/
        PR c++/15947
        * parser.c (cp_parser_template_name): Ctors/dtors never need a
        template keyword to disambiguate.

testsuite/
        PR c++/15947
        * g++.dg/parse/dtor4.C: New test.


Index: parser.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/parser.c,v
retrieving revision 1.192
diff -c -3 -p -r1.192 parser.c
*** parser.c 13 May 2004 06:40:19 -0000 1.192
--- parser.c 13 Jun 2004 02:14:58 -0000
*************** cp_parser_template_name (cp_parser* pars
*** 8172,8185 ****
        if (is_declaration
     && !template_keyword_p
     && parser->scope && TYPE_P (parser->scope)
!    && dependent_type_p (parser->scope))
   {
     ptrdiff_t start;
     cp_token* token;
     /* Explain what went wrong.  */
     error ("non-template `%D' used as template", identifier);
!    error ("(use `%T::template %D' to indicate that it is a template)",
!    parser->scope, identifier);
     /* If parsing tentatively, find the location of the "<"
        token.  */
     if (cp_parser_parsing_tentatively (parser)
--- 8184,8200 ----
        if (is_declaration
     && !template_keyword_p
     && parser->scope && TYPE_P (parser->scope)
!    && dependent_type_p (parser->scope)
!    /* Do not do this for dtors (or ctors), since they never
!       need the template keyword before their name.  */
!    && !constructor_name_p (identifier, parser->scope))
   {
     ptrdiff_t start;
     cp_token* token;
     /* Explain what went wrong.  */
     error ("non-template `%D' used as template", identifier);
!    inform ("use `%T::template %D' to indicate that it is a template",
!     parser->scope, identifier);
     /* If parsing tentatively, find the location of the "<"
        token.  */
     if (cp_parser_parsing_tentatively (parser)



// { dg-do compile }
// Contributed by Paul Koning <pkoning at equallogic dot com>
// PR c++/15947: Accept destructor as template in qualified-id

template <int N> struct X {
  ~X();
};

template <int N>
X<N>::~X<N>(){}





More information about the Gcc-patches mailing list