Re: some quirks of Koenig lookup rule?
  Home FAQ Contact Sign in
comp.lang.c++.moderated only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: some quirks of Koenig lookup rule?         

Group: comp.lang.c++.moderated · Group Profile
Author: Bo Persson
Date: May 6, 2008 11:14

Michael Kilburn wrote:
> Hi
>
> Can someone explain this peculiar behavior of MSVC & GCC:
> code below, if you uncomment that line (which is totally unrelated
> to 'dph' class), will stop compiling with usual bizzare C++ error
>
> [code]
> #include
>
> namespace AAA {
> struct gad
> {
> };
>
> //std::ostream& operator<<(std::ostream& s, gad const&);
>
> struct dph
> {
> void f();
> };
>
> }
>
> std::ostream& operator<<(std::ostream& s, AAA::dph const&)
> {
> return s;
> }
>
> void AAA::dph::f()
> {
> std::cout << *this;
> }
>
> int main(int argc, char* argv[])
> {
> return 1;
> }
> [/code]
>

The strange thing is to have an operator<< declared outside of the
namespace where dph is defined. Why would you do that?

Inside function f(), the compiler looks for an operator<< in the
namespace of dph. If you uncomment your line there, it will find one
(but not the right one).

The operator is not at all "totally unrelated" to dph - they are
declared in the same namespace. That's a strong relation!

Bo Persson

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
no comments
diggit! del.icio.us! reddit!