|
Prev: A proposal to cancel overriding the DEFAULT operator new/delete
Next: How to convert a pointer to member function type to a const member one
From: Bo Persson on 6 May 2008 03:47 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 <iostream> > > 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! ]
From: nickf3 on 6 May 2008 03:44 On May 6, 10:24 am, Michael Kilburn <crusader.m...(a)gmail.com> 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 <iostream> > > 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] > > Sincerely yours, > Michael. > Michael, Since names in different scopes do not participate in overloading the function AAA::operator<< hides the ::operator<< for types in namespace AAA. -- Nikolai [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Thomas Maeder on 6 May 2008 04:13
Michael Kilburn <crusader.mike(a)gmail.com> writes: > Can someone explain this peculiar behavior of MSVC & GCC: Yes. NB: Your question isn't related to argument dependant (aka Koenig) lookup. It's a basic overloading problem (if these exist at all). > code below, if you uncomment that line (which is totally unrelated to > 'dph' class), will stop compiling with usual bizzare C++ error It's unrelated to struct dph, but it's related to that type's operator<<(). > [code] > #include <iostream> > > 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; This statement causes the compiler to look for operator<<()s in a sequence of scopes. The first scope considered is the struct dph, which doesn't have an operator<<() member. The next scope considered is namespace AAA, where an operator<<() declaration is found. The compiler then determines that the parameters required by AAA::operator<<() don't match those you intend to pass. No further scope is then considered. The obvious fix is to move the other operator<<() into namespace AAA. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |