This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: friends inside namespace
- To: d-dooling at nwu dot edu
- Subject: Re: friends inside namespace
- From: Nathan Sidwell <nathan at acm dot org>
- Date: Fri, 05 Feb 1999 16:20:21 +0000
- CC: egcs-bugs at cygnus dot com
- Organization: University of Bristol
- References: <19990205152650.11638.qmail@piglet.chem-eng.nwu.edu>
- Reply-To: nathan at cs dot bris dot ac dot uk
David J. Dooling wrote:
>
> Brief description: declaring a friend function/operator inside a
> namespace and defining it outside the namespace (after a ``using''
> directive) causes the compiler to think two functions with the same
> signature have been created, one inside the namespace and one outside,
> leading to an ambiguity. If you do not use a using directive and
> fully qualify all functions/operators/classes, it compiles without
> error.
> friend.cc: In function `int main()':
> friend.cc:22: ambiguous overload for `_IO_ostream_withassign & << ::N::X &'
> friend.cc:15: candidates are: operator <<(ostream &, const ::N::X &)
> friend.cc:9: operator <<(ostream &, const ::N::X &)
The compiler is correct in issuing a diagnostic, but unfortunately removes
information from it which shows that lines 15 and 9 declare/define
different functions. the 19990131 snapshot provides the more informative
diagnostic,
foo.cc: In function `class ostream & operator <<(class ostream &, const class N::X &)':
foo.cc:16: member `i' is a private member of class `N::X'
foo.cc: In function `int main()':
foo.cc:22: ambiguous overload for `_IO_ostream_withassign & << N::X &'
foo.cc:15: candidates are: operator <<(ostream &, const N::X &)
foo.cc:9: N::operator <<(ostream &, const N::X &)
You are confused about the meaning of the using directive.
A friend declaration introduces a name into the namespace scope enclosing the
current class declaration (unless the name is qualified). So line 9 introduces
'N::operator<<'. The using declaration applies to name lookup, not name
declaration or definition. Therefore line 15 is defining a new function
'::operator<<', which is not a friend of N::X. Hence the diagnostic about
line 16. I am puzzled as to why you didn't get such a diagnostic too.
You need to change line 15 from unqualified 'operator<<' 'to `N::operator<<'.
nathan
--
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
You can up the bandwidth, but you can't up the speed of light
nathan@acm.org http://www.cs.bris.ac.uk/~nathan/ nathan@cs.bris.ac.uk