Language Subtlety or Compiler bug?

From: Ned Harding (nharding_at_extendthereach.com)
Date: 08/31/04


Date: 31 Aug 2004 10:25:03 -0700

In VC7.1 the following code outputs:
00000000
Success!

It would seem that the Y::operator<< is hiding the global operator<<
when it is outputting an object from another namespace but not when it
is outputting an object in the global namespace.

I can't help but think one of these 2 behaviors is a bug in VC7.1, but
which one?

Thanks,

Ned.

=========================================
#include <iostream>

namespace X
{
        class Abc1
        {
        };

}
std::ostream& operator<< (std::ostream& strm, const X::Abc1 *node)
{
        return strm << "Success!";
}

X::Abc1 * GetDoc() { return NULL; }

class Abc2
{
};

Abc2 * GetDoc2() { return NULL; }

std::ostream& operator<< (std::ostream& strm, const Abc2 *node)
{
        return strm << "Success!";
}

namespace Y
{
        std::ostream & operator<<(std::ostream& a, const char& b)
        {
                return a << "This should not run";
        }

        void DoTest()
        {
                std::cout << GetDoc() << "\n";
                std::cout << GetDoc2() << "\n";
        }
}

int main(int argc, char* argv[])
{
        Y::DoTest();
        return 0;
}



Relevant Pages

  • Re: Language Subtlety or Compiler bug?
    ... > In VC7.1 the following code outputs: ... > is outputting an object in the global namespace. ... In the case where you pass an Abc1*, however, your ...
    (comp.lang.cpp)
  • Re: Language Subtlety or Compiler bug?
    ... > is outputting an object in the global namespace. ... It doesn't look in the global namespace because none of its ... std::cout << (something with char type or convertible to char). ...
    (comp.lang.cpp)
  • Re: include headers: <X>, <X.h> and global namespaces
    ... > keep standard library definitions out of the global namespace. ... C++ standard says nothing at all about what it will or will not do. ... Each name declared as an object with external linkage in a header is ...
    (comp.lang.cpp)
  • Re: Bug in execfile?
    ... If you execfilea script names not known in the context of a function are ... looked up in the global namespace, but assignments set names in the local ... x and f are put into the local namespace, but x is not local to the function ... Now if you call execfilein the global namespace of the client script the ...
    (comp.lang.python)
  • Re: Lambda forms and scoping
    ... A function carries its own local namespace, its own closure, and its ... a global statement) the global namespace. ...
    (comp.lang.python)