<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: C++ Learning Note(5): Koenig Lookup</title>
	<atom:link href="http://kunxi.org/archives/2007/04/c-learning-note2-koenig-lookup/feed/" rel="self" type="application/rss+xml" />
	<link>http://kunxi.org/archives/2007/04/c-learning-note2-koenig-lookup/</link>
	<description>Yet another code monkey blog.</description>
	<lastBuildDate>Wed, 04 Jan 2012 07:28:05 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Eden Crane</title>
		<link>http://kunxi.org/archives/2007/04/c-learning-note2-koenig-lookup/comment-page-1/#comment-55244</link>
		<dc:creator>Eden Crane</dc:creator>
		<pubDate>Fri, 31 Oct 2008 22:46:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.kunxi.org/archives/2007/03/c-learning-note2-koenig-lookup/#comment-55244</guid>
		<description>to mimic the magic, along with your function &#039;f&#039; INSIDE of the NS, you need a function &#039;f&#039; OUTSIDE of NS.

Forget about the &#039;g&#039; function for a minute and take a look at the following code:


#include 
using namespace std;

namespace NS {
        class A{};
        void f(A) { cout &lt;&lt; &quot;f INSIDE called&quot; &lt;&lt; endl; }
}

template
void f(T) { cout &lt;&lt; &quot;f OUTSIDE is called&quot; &lt;&lt; endl; }


int main()
{
        NS::A a;
        f(a);
}


What is the expected output of this code? We have 2 functions named &#039;f&#039;, and in &#039;main&#039;  we are only using the namespace &#039;std&#039;. A beginning C++ programmer would expect the OUTSIDE version of &#039;f&#039; to be called. 

Run the program, output is:
&lt;i&gt;f INSIDE is called&lt;/i&gt;

When we call f(A), because the A class resides in the NS namespace, the NS namespace is checked for a function f(A) where a match is found. When we called f(A) we did not specify the NS namespace at all, but thanks to Koenig lookup, the NS namespace version was used.


With regards to the Wikipedia comment, here is the concern of that writer. Let&#039;s say you want to write a special version of std::swap for your class, to make the operation more efficient. 

#include 
using namespace std;

namespace NS {
        class A{};
        void f(A) { cout &lt;&lt; &quot;f INSIDE called&quot; &lt;&lt; endl; }
        void swap(A&amp; one,A&amp; two) { cout &lt;&lt; &quot;swap INSIDE called&quot;;}
}

template
void f(T) { cout &lt;&lt; &quot;f OUTSIDE is called&quot; &lt;&lt; endl; }


int main()
{
        NS::A a;
        f(a);

        NS::A b;
        swap(a,b);
}


Output is:
&lt;i&gt;f INSIDE called
cout INSIDE called&lt;/i&gt;

Thanks to Koenig lookup, your version of &#039;swap&#039; inside of the NS namespace will be called, even though the program is using the std namespace. The output of this program is:



The problem is, some people like to use fully-qualified names. If we take the above code, remove the &#039;using namespace std&#039; and replace all calls to the std namespace, we get:

#include 

namespace NS {
        class A{};
        void f(A) { std::cout &lt;&lt; &quot;f INSIDE called&quot; &lt;&lt; std::endl; }
        void swap(A&amp; one,A&amp; two) { std::cout &lt;&lt; &quot;swap INSIDE called&quot; &lt;&lt; std::endl;}
}

template
void f(T) { std::cout &lt;&lt; &quot;f OUTSIDE is called&quot; &lt;&lt; std::endl; }

int main()
{
        NS::A a;
        f(a);

        NS::A b;
        std::swap(a,b);
}

Output is:
&lt;i&gt;f INSIDE called&lt;/i&gt;

In this case, the fears of the wikipedia writer are realized. We have one programmer who designed the NS namespace and thought that he could slip in an alternate version of the std::swap function call. It works if users of the NS namespace use /unqualified/ calls to swap, but it fails to work if the users of the NS namespace use /fully-qualified/ calls to std::swap.</description>
		<content:encoded><![CDATA[<p>to mimic the magic, along with your function &#8216;f&#8217; INSIDE of the NS, you need a function &#8216;f&#8217; OUTSIDE of NS.</p>
<p>Forget about the &#8216;g&#8217; function for a minute and take a look at the following code:</p>
<p>#include<br />
using namespace std;</p>
<p>namespace NS {<br />
        class A{};<br />
        void f(A) { cout &lt;&lt; &#8220;f INSIDE called&#8221; &lt;&lt; endl; }<br />
}</p>
<p>template<br />
void f(T) { cout &lt;&lt; &#8220;f OUTSIDE is called&#8221; &lt;&lt; endl; }</p>
<p>int main()<br />
{<br />
        NS::A a;<br />
        f(a);<br />
}</p>
<p>What is the expected output of this code? We have 2 functions named &#8216;f&#8217;, and in &#8216;main&#8217;  we are only using the namespace &#8216;std&#8217;. A beginning C++ programmer would expect the OUTSIDE version of &#8216;f&#8217; to be called. </p>
<p>Run the program, output is:<br />
<i>f INSIDE is called</i></p>
<p>When we call f(A), because the A class resides in the NS namespace, the NS namespace is checked for a function f(A) where a match is found. When we called f(A) we did not specify the NS namespace at all, but thanks to Koenig lookup, the NS namespace version was used.</p>
<p>With regards to the Wikipedia comment, here is the concern of that writer. Let&#8217;s say you want to write a special version of std::swap for your class, to make the operation more efficient. </p>
<p>#include<br />
using namespace std;</p>
<p>namespace NS {<br />
        class A{};<br />
        void f(A) { cout &lt;&lt; &#8220;f INSIDE called&#8221; &lt;&lt; endl; }<br />
        void swap(A&amp; one,A&amp; two) { cout &lt;&lt; &#8220;swap INSIDE called&#8221;;}<br />
}</p>
<p>template<br />
void f(T) { cout &lt;&lt; &#8220;f OUTSIDE is called&#8221; &lt;&lt; endl; }</p>
<p>int main()<br />
{<br />
        NS::A a;<br />
        f(a);</p>
<p>        NS::A b;<br />
        swap(a,b);<br />
}</p>
<p>Output is:<br />
<i>f INSIDE called<br />
cout INSIDE called</i></p>
<p>Thanks to Koenig lookup, your version of &#8216;swap&#8217; inside of the NS namespace will be called, even though the program is using the std namespace. The output of this program is:</p>
<p>The problem is, some people like to use fully-qualified names. If we take the above code, remove the &#8216;using namespace std&#8217; and replace all calls to the std namespace, we get:</p>
<p>#include </p>
<p>namespace NS {<br />
        class A{};<br />
        void f(A) { std::cout &lt;&lt; &#8220;f INSIDE called&#8221; &lt;&lt; std::endl; }<br />
        void swap(A&amp; one,A&amp; two) { std::cout &lt;&lt; &#8220;swap INSIDE called&#8221; &lt;&lt; std::endl;}<br />
}</p>
<p>template<br />
void f(T) { std::cout &lt;&lt; &#8220;f OUTSIDE is called&#8221; &lt;&lt; std::endl; }</p>
<p>int main()<br />
{<br />
        NS::A a;<br />
        f(a);</p>
<p>        NS::A b;<br />
        std::swap(a,b);<br />
}</p>
<p>Output is:<br />
<i>f INSIDE called</i></p>
<p>In this case, the fears of the wikipedia writer are realized. We have one programmer who designed the NS namespace and thought that he could slip in an alternate version of the std::swap function call. It works if users of the NS namespace use /unqualified/ calls to swap, but it fails to work if the users of the NS namespace use /fully-qualified/ calls to std::swap.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Moreshwar</title>
		<link>http://kunxi.org/archives/2007/04/c-learning-note2-koenig-lookup/comment-page-1/#comment-28677</link>
		<dc:creator>Moreshwar</dc:creator>
		<pubDate>Sun, 28 Oct 2007 04:36:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.kunxi.org/archives/2007/03/c-learning-note2-koenig-lookup/#comment-28677</guid>
		<description>No, I think everything is correct in this case. 
Consider what happens here : 

void foo()
{
  int bar = 0; 
  {
    int bar = 10; 
    cout &lt;&lt; bar &lt;&lt; endl;
  }
}

I think this should print 10 and not 0. This is simple scope resolution and hiding principles right?</description>
		<content:encoded><![CDATA[<p>No, I think everything is correct in this case.<br />
Consider what happens here : </p>
<p>void foo()<br />
{<br />
  int bar = 0;<br />
  {<br />
    int bar = 10;<br />
    cout &lt;&lt; bar &lt;&lt; endl;<br />
  }<br />
}</p>
<p>I think this should print 10 and not 0. This is simple scope resolution and hiding principles right?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

