<?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[verschachtelte Klassen, templates &amp;amp; friends]]></title><description><![CDATA[<p>Hallo,</p>
<p>der folgende Code kompiliert nicht:</p>
<pre><code>template &lt;typename T&gt;
class Q
{
   public:
      class R
      {
         template &lt;typename U&gt;
         friend bool operator==(const typename Q&lt;U&gt;::R&amp; a, const typename Q&lt;U&gt;::R&amp; b);
      };
};

template &lt;typename U&gt;
bool operator==(const typename Q&lt;U&gt;::R&amp; a, const typename Q&lt;U&gt;::R&amp; b)
{
   return true;
}

int main()
{
   Q&lt;int&gt;::R a, b;
   a == b;
}
</code></pre>
<pre><code>21:6: error: invalid operands to binary expression ('Q&lt;int&gt;::R' and 'Q&lt;int&gt;::R')
13:6: note: candidate template ignored: couldn't infer template argument 'U'
</code></pre>
<p>Wie kann ich den Fehler beheben?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/324502/verschachtelte-klassen-templates-amp-friends</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Jul 2026 08:40:16 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/324502.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 19 Mar 2014 17:31:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to verschachtelte Klassen, templates &amp;amp; friends on Wed, 19 Mar 2014 17:32:06 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>der folgende Code kompiliert nicht:</p>
<pre><code>template &lt;typename T&gt;
class Q
{
   public:
      class R
      {
         template &lt;typename U&gt;
         friend bool operator==(const typename Q&lt;U&gt;::R&amp; a, const typename Q&lt;U&gt;::R&amp; b);
      };
};

template &lt;typename U&gt;
bool operator==(const typename Q&lt;U&gt;::R&amp; a, const typename Q&lt;U&gt;::R&amp; b)
{
   return true;
}

int main()
{
   Q&lt;int&gt;::R a, b;
   a == b;
}
</code></pre>
<pre><code>21:6: error: invalid operands to binary expression ('Q&lt;int&gt;::R' and 'Q&lt;int&gt;::R')
13:6: note: candidate template ignored: couldn't infer template argument 'U'
</code></pre>
<p>Wie kann ich den Fehler beheben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389776</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389776</guid><dc:creator><![CDATA[mortified_penguin]]></dc:creator><pubDate>Wed, 19 Mar 2014 17:32:06 GMT</pubDate></item><item><title><![CDATA[Reply to verschachtelte Klassen, templates &amp;amp; friends on Wed, 19 Mar 2014 17:47:21 GMT]]></title><description><![CDATA[<p>Das funktioniert nicht, weil das Template-Argument in dem nested-name-specifier vorkommt. Das ist ein sog. 'non-deduced context'.</p>
<blockquote>
<p>If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.<br />
The non-deduced contexts are:<br />
— The <em>nested-name-specifier</em> of a type that was specified using a <em>qualified-id</em>.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2389781</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389781</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 19 Mar 2014 17:47:21 GMT</pubDate></item><item><title><![CDATA[Reply to verschachtelte Klassen, templates &amp;amp; friends on Wed, 19 Mar 2014 17:49:44 GMT]]></title><description><![CDATA[<p>Der kann den Templateparameter U nicht auflösen, weil der nicht genau weiß wie sich die Klassen bei verschiedenen Spezialisierungen verhalten.<br />
Lösung:</p>
<pre><code>template &lt;typename T&gt;
class Q
{
   public:
      class R
      {
         friend bool operator==(const typename Q&lt;T&gt;::R&amp; a, const typename Q&lt;T&gt;::R&amp; b)
         {
   				return true;
			}
      };
};
</code></pre>
<p>operator== gehört somit direkt zu R und kann via ADL gefunden werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389783</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389783</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Wed, 19 Mar 2014 17:49:44 GMT</pubDate></item><item><title><![CDATA[Reply to verschachtelte Klassen, templates &amp;amp; friends on Wed, 19 Mar 2014 17:50:22 GMT]]></title><description><![CDATA[<p>Ok, das macht auch irgendwie Sinn. Also ist die einzige Möglichkeit die Funktion direkt in der Klasse zu definieren?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389784</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389784</guid><dc:creator><![CDATA[mortified_penguin]]></dc:creator><pubDate>Wed, 19 Mar 2014 17:50:22 GMT</pubDate></item><item><title><![CDATA[Reply to verschachtelte Klassen, templates &amp;amp; friends on Wed, 19 Mar 2014 17:52:30 GMT]]></title><description><![CDATA[<p>Ein einfacher Fix dafür ist</p>
<pre><code>template &lt;typename T&gt;
struct Q
{
	struct R
	{
		friend bool operator==(const R&amp; a, const R&amp; b) { return true; }
	};
};

int main()
{
	Q&lt;int&gt;::R a, b;
	a == b;
}
</code></pre>
<p>Die Operatorfunktion wird dabei durch ADL gefunden und ist kein Template.</p>
<p>Edit: Das gibt es gar nicht, da schreibe ich glatt genau was Nathan schon schrieb...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389786</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389786</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 19 Mar 2014 17:52:30 GMT</pubDate></item><item><title><![CDATA[Reply to verschachtelte Klassen, templates &amp;amp; friends on Wed, 19 Mar 2014 18:07:27 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>Das gibt es gar nicht, da schreibe ich glatt genau was Nathan schon schrieb...</p>
</blockquote>
<p>:p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389791</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389791</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Wed, 19 Mar 2014 18:07:27 GMT</pubDate></item></channel></rss>