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: friends inside namespace


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


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