gcc 2.95.1 fails to compile correct code

Martin v. Loewis martin@mira.isdn.cs.tu-berlin.de
Fri Sep 10 14:22:00 GMT 1999


> 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); // ill­formed ± 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


More information about the Gcc-bugs mailing list