C++ PATCH: PR 11493 and PR 11495

Wolfgang Bangerth bangerth@ices.utexas.edu
Fri Jul 18 15:15:00 GMT 2003


> Suggestion 2: Add the following reduced variant of my example to
> trouble.texi, together with the (updated) error message.
>
> ----- cut -----
> template <class T>
> struct D : T {
>   void dosomething() {
>     f();         // problem
>     this->f();   // fix variant 1
>     T::f();      // fix variant 2
>   }
> };
> ----- cut -----

Below is a patch for this. Let me know if this is what people think is what we 
want. Mark: is the description of what -fpermissive does in this case 
accurate? I should state that I'm not quite happy with what -fpermissive 
presently does, since it only addresses the case of unqualified _function_ 
name lookup, but the respective case for variables is not covered. That seems 
rather inconsistent.

I have added a note to the place where this message is generated, to state 
that the wording in the message is copied in the manual and that they should 
be kept in synch. Is this appropriate?

Mark, second question: in the section above what is changed in the patch, we 
state 
  This distinction between lookup of dependent and non-dependent names is
  called two-stage (or dependent) name lookup. G++ implements some
  features of it since version 3.4 and is moving towards full compliance
  with the standard.
I have the impression that we try to do it _all_ right already now. Should 
this be reworded thus?

W.


Index: doc/trouble.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/trouble.texi,v
retrieving revision 1.15
diff -c -r1.15 trouble.texi
*** doc/trouble.texi    7 Feb 2003 23:12:03 -0000       1.15
--- doc/trouble.texi    18 Jul 2003 15:11:33 -0000
***************
*** 1,5 ****
  @c Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
! @c 1999, 2000, 2001 Free Software Foundation, Inc.
  @c This is part of the GCC manual.
  @c For copying conditions, see the file gcc.texi.
  
--- 1,5 ----
  @c Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
! @c 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
  @c This is part of the GCC manual.
  @c For copying conditions, see the file gcc.texi.
  
***************
*** 974,983 ****
  @code{Base<T>::i}. Alternatively, @code{Base<T>::i} might be brought
  into scope by a @code{using}-declaration.
  
! Note that some compilers get this wrong and accept above code without an
! error.  However, this is spurious, since they just don't implement
! two-stage name lookup correctly.  This includes G++ versions prior to
! 3.4.
  
  
  @node Temporaries
--- 974,1023 ----
  @code{Base<T>::i}. Alternatively, @code{Base<T>::i} might be brought
  into scope by a @code{using}-declaration.
  
! Another, similar example involves calling member functions of the base
! class: 
! 
! @example
!   template <typename T> struct Base {
!       int f();
!   };
! 
!   template <typename T> struct Derived : Base<T> {
!       int g() { return f(); };
!   };
! @end example
! 
! Again, the call to @code{f()} is not dependent on template arguments
! (none of the arguments -- of which there are none anyway here -- depend
! on the type @code{T}, and it is also not otherwise specified that the
! call should be in a dependent context), so a global declaration of such
! a function must be available, since the one in the base class is not
! visible until instantiation time.  The compiler will thus produce the
! following error message:
! 
! @example
!   x.cc: In member function `int Derived<T>::g()':
!   x.cc:6: error: there are no arguments to `f' that depend on a template
!      parameter, so a declaration of `f' must be available
!   x.cc:6: error: (if you use `-fpermissive', G++ will accept your code, but
!      allowing the use of an undeclared name is deprecated)
! @end example
! 
! The error can be removed by making the code valid: either write the call
! as @code{this->f()}, or @code{Base<T>::f()}.  Using the
! @code{-fpermissive} flag will also let the compiler accept the code, by
! marking all function calls for which no declaration is visible at the
! time of definition of the template for later lookup at instantiation
! time, as if it were a dependent call. We do not recommend using this
! flag for this purpose, since it will generate lots of warnings, and also
! because it will only catch cases where functions in base classes are
! called, not where variables in base classes are used (as in the example
! above).
! 
! Note that some compilers get these examples wrong and accept above code
! without an error.  However, this is spurious, since they just don't
! implement two-stage name lookup correctly.  This includes G++ versions
! prior to 3.4.
  
  
  @node Temporaries
Index: cp/lex.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/lex.c,v
retrieving revision 1.312
diff -c -r1.312 lex.c
*** cp/lex.c    16 Jul 2003 00:09:45 -0000      1.312
--- cp/lex.c    18 Jul 2003 15:11:33 -0000
***************
*** 708,715 ****
      {
        /* In a template, it is invalid to write "f()" or "f(3)" if no
         declaration of "f" is available.  Historically, G++ and most
!        other compilers accepted that usage; explain to the user what
!        is going wrong.  */
        pedwarn ("there are no arguments to `%D' that depend on a template "
               "parameter, so a declaration of `%D' must be available", 
               name, name);
--- 708,721 ----
      {
        /* In a template, it is invalid to write "f()" or "f(3)" if no
         declaration of "f" is available.  Historically, G++ and most
!        other compilers accepted that usage since they deferred all name
!        lookup until instantiation time rather than doing unqualified
!        name lookup at template definition time; explain to the user what 
!        is going wrong.
! 
!        Note that we have the exact wording of the following message in
!        the manual (trouble.texi, node "Name lookup"), so they need to
!        be kept in synch.  */
        pedwarn ("there are no arguments to `%D' that depend on a template "
               "parameter, so a declaration of `%D' must be available", 
               name, name);



More information about the Gcc-patches mailing list