Looks like a g++ bug --- apparently no namespace injection

Oliver Kullmann O.Kullmann@swansea.ac.uk
Sat Feb 27 22:44:00 GMT 2010


Hi Axel,

ADL according to the standard is applicable in this case,
and then overload resolution should prefer the more
specialised function.

So it seems that gcc is wrong here.

Thanks for your examples!

Oliver


On Wed, Feb 24, 2010 at 09:15:09PM +0100, Axel Freyn wrote:
> Hi Oliver,
> 
> Just a remark
> > 
> > #include <iostream>
> > struct X {
> >   struct Node {};
> >   friend std::ostream& operator <<(std::ostream& out, Node*) {
> >     return out << "This should be printed!";
> >   }
> > };
> > int main () {
> >   std::cout << (X::Node*) 0 << "\n";
> > }
> > 
> > I think this should print out the message, but g++ version 4.1.2 and 4.3.1
> > both print 0. If the <<-function is placed in the global namespace,
> > then it works.
> Well, this touches the huge secrets of the overload resolution of C++
> (which I don't understand completely...)
> I just tried a similar code:
> 
> #include <iostream>
> struct X {
>     struct Node {};
>     friend  std::ostream& operator <<(std::ostream& out, X::Node*) {
>       return out << "This should be printed!";
>     }
>     friend  std::ostream& operator <<(std::ostream& out, X::Node& n) {
>       return out << "This should be printed!";
>     }
> };
> int main () {
>     std::cout << (X::Node*) 0 << "\n";
>     X::Node n;
>     std::cout << &n << "\n";
>     std::cout << n << "\n";
> }
> 
> 
> That prints for me:
> 0
> 0xbfab3ef3
> This should be printed!
> 
> 1. line: that's your observation
> 2. line: &n gives me a pointer to "n", the pointer being of type
>          "X::Node *". This is now printed as a Pointer...
> 3. line: Finally, the output of an "X::Node" n works as expected.
> 
> So, as gcc finds the second function (to output "X::Node &n) I assume
> the problem to be in the overload resolution - your friend-function
> against the routines in the standard library... And there I have no
> idea, what would be the right answer in this situation.
> At least, if I remember correctly, if you define a friend-function
> inline, it is part of the lexical scope of the class - and not of the
> global scope.
> 
> 
> 
> Axel



More information about the Gcc-help mailing list