<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[friend, template und namespaces]]></title><description><![CDATA[<p>Hallo,</p>
<p>folgendes Problem beschäftigt mich: Ich möchte eine function template zum friend einer Klasse N::A machen:</p>
<pre><code class="language-cpp">// forward declare A

namespace N {
    class A;
}

// forward declare x

template&lt;typename T&gt;
void x(T, N::A&amp; a);

// declare A

namespace N {
    class A {
        private:
            int id;

            template&lt;typename T&gt;
            friend void ::x(T, A&amp;);
    };
}

// define x

template&lt;typename T&gt;
void x(T, N::A&amp; a) {
    a.id = 20;
}

// use

int main() {
    N::A a;
    ::x(0l, a);
}
</code></pre>
<p>Dies funktioniert aber mit gcc nicht.</p>
<pre><code>test.cc: In function ‘void x(T, N::A&amp;) [with T = long int]’:
test.cc:36:   instantiated from here
test.cc:17: error: ‘int N::A::id’ is private
test.cc:28: error: within this context
</code></pre>
<p>Wenn ich x aber in ein Namespace M übersiedle, und Zeile 20 wie folgt ändere, dann funktionierts:</p>
<pre><code class="language-cpp">friend void M::x(T, A&amp;);
</code></pre>
<p>Hat irgendwer einen Tipp wie ich das hinkriegen könnte das x im globalem Namespace bleibt?</p>
<p>danke<br />
Peter</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/182838/friend-template-und-namespaces</link><generator>RSS for Node</generator><lastBuildDate>Wed, 23 Sep 2026 01:04:06 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/182838.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 29 May 2007 19:41:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to friend, template und namespaces on Tue, 29 May 2007 19:41:11 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>folgendes Problem beschäftigt mich: Ich möchte eine function template zum friend einer Klasse N::A machen:</p>
<pre><code class="language-cpp">// forward declare A

namespace N {
    class A;
}

// forward declare x

template&lt;typename T&gt;
void x(T, N::A&amp; a);

// declare A

namespace N {
    class A {
        private:
            int id;

            template&lt;typename T&gt;
            friend void ::x(T, A&amp;);
    };
}

// define x

template&lt;typename T&gt;
void x(T, N::A&amp; a) {
    a.id = 20;
}

// use

int main() {
    N::A a;
    ::x(0l, a);
}
</code></pre>
<p>Dies funktioniert aber mit gcc nicht.</p>
<pre><code>test.cc: In function ‘void x(T, N::A&amp;) [with T = long int]’:
test.cc:36:   instantiated from here
test.cc:17: error: ‘int N::A::id’ is private
test.cc:28: error: within this context
</code></pre>
<p>Wenn ich x aber in ein Namespace M übersiedle, und Zeile 20 wie folgt ändere, dann funktionierts:</p>
<pre><code class="language-cpp">friend void M::x(T, A&amp;);
</code></pre>
<p>Hat irgendwer einen Tipp wie ich das hinkriegen könnte das x im globalem Namespace bleibt?</p>
<p>danke<br />
Peter</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1294769</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1294769</guid><dc:creator><![CDATA[ubu_roi]]></dc:creator><pubDate>Tue, 29 May 2007 19:41:11 GMT</pubDate></item><item><title><![CDATA[Reply to friend, template und namespaces on Wed, 30 May 2007 11:51:10 GMT]]></title><description><![CDATA[<p>Hi, dein Problem ist folgendes:</p>
<p>Erstens deklarierst du eine Funktion x hier:</p>
<pre><code class="language-cpp">// forward declare x

template&lt;typename T&gt;
void x(T, N::A&amp; a);
</code></pre>
<p>Danach auch wieder hier:</p>
<pre><code class="language-cpp">template&lt;typename T&gt;
            friend void ::x(T, A&amp;);
</code></pre>
<p>Würdest du hier:</p>
<pre><code class="language-cpp">N::A a;
::x(0l, a);
</code></pre>
<p>Das :: vor x weglassen, würdest du merken das dies Mehrdeutig ist.<br />
Das kommt durch den &quot;Koenig-Lookups&quot;, der besagt falls man ein Argument N::A an eine Funktion übergibt, dass auch in N nach der Funktion geschaut wird, zudem gibt es auch nochmal die globale Funktion x;</p>
<p>Lösung des Problems:<br />
-&gt; // forward declare x weg<br />
-&gt; template&lt;typename T&gt; friend void x(T, A&amp;); // also ohne ::<br />
-&gt; und bei der Def von x wieder N::x { definiton }</p>
<p>Edit: Also so:</p>
<pre><code class="language-cpp">namespace N {
    class A;
}

// declare A

namespace N {
    class A {
        private:
            int id;
        public:
            template&lt;typename T&gt; friend void x(T, A&amp;);
    };
}

// define x

template&lt;typename T&gt;
void N::x(T, N::A&amp; a) {
    a.id = 20;
}

// use

int main() {
    N::A a;
    x(0l, a);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1294821</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1294821</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Wed, 30 May 2007 11:51:10 GMT</pubDate></item><item><title><![CDATA[Reply to friend, template und namespaces on Tue, 29 May 2007 20:57:52 GMT]]></title><description><![CDATA[<p>Hallo KasF,</p>
<p>danke für die Antwort, es compiliert! Was mich allerdings wundert, ist wieso ich<br />
in main</p>
<pre><code class="language-cpp">x(0l, a);
</code></pre>
<p>aufrufen kann, wenn doch x im namespace N liegt:</p>
<pre><code class="language-cpp">template&lt;typename T&gt;
void N::x(T, N::A&amp; a) { 
...
</code></pre>
<p>Oder verstehe ich da was falsch?</p>
<p>Peter</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1294833</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1294833</guid><dc:creator><![CDATA[ubu_roi]]></dc:creator><pubDate>Tue, 29 May 2007 20:57:52 GMT</pubDate></item><item><title><![CDATA[Reply to friend, template und namespaces on Tue, 29 May 2007 21:05:09 GMT]]></title><description><![CDATA[<p>ubu_roi schrieb:</p>
<blockquote>
<pre><code class="language-cpp">x(0l, a);
</code></pre>
</blockquote>
<p>Wie schon erwähnt, hat das mit dem &quot;Koenig Lookup&quot; zu tun. Da dein Argument a vom Typ N::A ist, wird auch in N nach einer Funktion x gesucht.</p>
<p>Näheres dazu hier: <a href="http://fara.cs.uni-potsdam.de/~kaufmann/?page=GenCppFaqs&amp;faq=Koenig#Answ" rel="nofollow">http://fara.cs.uni-potsdam.de/~kaufmann/?page=GenCppFaqs&amp;faq=Koenig#Answ</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1294838</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1294838</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Tue, 29 May 2007 21:05:09 GMT</pubDate></item></channel></rss>