TITLE: namespaces and operator overloading resolution (Newsgroups: comp.lang.c++, 4 Jun 97) KWEE: Patrick Kwee > I have some problems namespaces : I have defined a class in a > namespace and I also defined an op<< for ostreams. When I now try > to output ints or const chars my compiler says, that such an op<< > was not defined for these types. Does the compiler not search in > other namespaces in this case in the global namespace for the > standard op<< ? BECKER: Pete Becker This is tricky. The following should work: namespace n { class C { }; }; ostream& operator << ( ostream&, const n::C& ); n::C nc; cout << nc; That is, the compiler should look in the global namespace for the appropriate operator. The tricky part is that this should also work: namespace n { class C { }; ostream& operator << ( ostream&, const n::C& ); }; n::C nc; cout << nc; Now the << operator is defined in the namespace n, but there is nothing in the invocation of << that says that it lives in namespace n. The rule is that the compiler looks in the namespace where C is defined to find operators that can act on C. Of course, if there's nothing applicable there, it should still look in the global namespace. In general, though, I'd wouldn't expect operators and namespaces to work together well for a while yet. The issues are harder than they look, and it's going to be a while before compilers get them right. [snip]