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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux