friends in template class or gcc bug ?

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Tue Mar 14 12:43:00 GMT 2000


> what's the problem with the following code ?

It's ill-formed.

> what does this mean ? I can't understand ? as I can read from the 
> standard the above is a correct code, couldn't ?
> look at stroustrup's book page 347 and 855 or in the 
> "1997 C++ Public Review Document" 14.5.3.

Well, the reference to in the gcc manual was to section 14.5.3 of the
ISO C++ standard (ISO/IEC 14882:1998); the draft documents might be
different.

Anyway, in 14.5.3, it says

# For a friend function declaration that is not a template
# declaration:

Yes, this applies here:

  friend ostream& operator<<(ostream&, const A&);

That is a function declaration, but not a template declaration.

# if the name of the friend is a qualified or unqualified template­id,
# ...

No, that is not the case.

# if the name of the friend is a qualified­id and a matching
# nontemplate function is found

No, that is also not the case

# if the name of the friend is a qualified­id and a matching
# specialization of a template function is found

Neither is this

# the name shall be an unqualified­id that declares (or redeclares) an
# ordinary (nontemplate) function.

Since all else failed, we now know that the operator<< must be an
ordinary, non-template function. Since you are instantiating A<int>,
the signature of this friend will be

  ostream& operator<<(ostream&, const A<int>&);

(as a normal function *not* as a template). Since you do not implement
that function, you get a linker error.

If you question this line of reasoning, please discuss it in one of
the public C++ fora first, eg. comp.lang.c++.moderated, or
comp.std.c++.

> "The only thing worse than not knowing the truth is
>  ruining the bliss of ignorance."

Wisely spoken :-)


Regards,
Martin


More information about the Gcc-bugs mailing list