This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
template with member and friend function having the same name
- To: gcc at gcc dot gnu dot org
- Subject: template with member and friend function having the same name
- From: Vladimir Yelistratov <yelistratov at gmd dot de>
- Date: Wed, 22 Mar 2000 14:21:05 +0100
- Organization: GMD
Hi !
Is it really true that member and friend functions of a template can not
have the same name?
It is very convient to have operator* as member function and as friend
function
in template dealing with multiplication of vectors and scalars.
This topic is also mentioned in
http://gcc.gnu.org/ml/gcc-bugs/1999-09n/msg00320.html
Will be the bug for theoretically correct definition
friend A<T> ::operator- <> (const T, const A<T>&);
fixed in next releases of gcc?
Thanks,
Vladimir
**** from http://gcc.gnu.org/ml/gcc-bugs/1999-09n/msg00320.html:
> EGCS 1.1.1 compiles this, I'm pretty sure it's correct.
Yet, it is not. If you have a copy of the standard, please have a look
at the example in 14.5.3/2. It contains the case in question:
class ... {
template <class T> void i(T);
friend void i<>(int); // illformed ± A::i
};
> template <class T> struct A {
> A<T> operator- (const A<T>& ) const;
>
> friend A<T> operator- <> (const T, const A<T>&);
> };
The problem is that operator- in the friend declaration here refers to
A::operator-, instead of the global one, so your code is ill-formed.
In theory, you could solve this by writing
friend A<T> ::operator- <> (const T, const A<T>&);
but g++ does not accept this - this is a known bug.
The work-around is to write
template<class X>
friend A<X> operator- (const X, const A<X>&);
instead. Yes, this does have a slightly different semantics.
Hope this helps,
Maritn