This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: g++-2.95.2 bug: can't have friend template function w/ same name/sig as member


> The following code does not compile if the template function foo()
> has the same name and signature as a member of class bar, and is
> also declared a friend of class bar:

Thanks for your bug report. The mainline compiler now says

a.cc:11: declaration of `bar<T> bar<T>::foo (T) const'
a.cc:4: changes meaning of `foo' from `bar<T> foo (const bar<T> &, T)'
a.cc: In instantiation of `bar<int>':
a.cc:14:   instantiated from here
a.cc:9: invalid use of undefined type `class bar<int>'
a.cc:8: forward declaration of `class bar<int>'

which gives a more detailed description of the problem. When you make
the friend declaration, the compiler has to perform lookup to find a
prior declaration of the friend, which is necessary to determine which
function is declared friend (7.3.1.2).

In your example, the lookup is performed for the friend starting from
class scope. Later, you introduce another declaration in class scope,
thus changing the binding of the symbol to a member. This is an error
(3.3.6/1.2). To correct your example, you'd have to write

template <class T> struct bar; 

template <class T> 
bar<T> foo (const bar<T>&, T); 

template <class T> 
struct bar 
{ 
    friend bar ::foo<> (const bar&, T); 

    bar<T> foo (T) const; 
}; 

template class bar<int>; 

i.e. qualifying the friend. Unfortunately, namespace-qualifying
friends does not work currently in g++; that's a separate bug.

Please have a look at Core Issue 138
(http://www.informatik.hu-berlin.de/~loewis/corer8.html#138);
apparently, the procedure to find a 'prior declaration' is unclear; it
is also unclear whether the procedure has to be applied for
template-ids (issue 166).

Regards,
Martin

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]