Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: some quirks...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 5 Topic 9572 of 9831
Post > Topic >>

Re: some quirks of Koenig lookup rule?

by "Bo Persson" <bop@[EMAIL PROTECTED] > May 6, 2008 at 12:47 PM

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! ]
 




 5 Posts in Topic:
some quirks of Koenig lookup rule?
Michael Kilburn <crusa  2008-05-06 08:24:11 
Re: some quirks of Koenig lookup rule?
"Bo Persson" &l  2008-05-06 12:47:56 
Re: some quirks of Koenig lookup rule?
nickf3 <nickf3@[EMAIL   2008-05-06 12:44:45 
Re: some quirks of Koenig lookup rule?
Thomas Maeder <maeder@  2008-05-06 13:13:26 
Re: some quirks of Koenig lookup rule?
dizzy <dizzy@[EMAIL PR  2008-05-07 11:43:27 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 3:04:58 CDT 2008.