<?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[klassentemplate in klassentemplate]]></title><description><![CDATA[<p>hallo</p>
<p>ich versuche gerade einen code im stil von dem hier zum laufen zu bekommen (minimalbeispiel):</p>
<pre><code>template&lt;class T&gt;
struct foo
{
	template&lt;class U&gt;
	struct bar
	{
		int val;
		bar(int n) : val(n) {}
	};
};
template&lt;class T, class U&gt;
foo&lt;T&gt;::bar&lt;U&gt; operator+ (int lhs, foo&lt;T&gt;::bar&lt;U&gt; rhs)
{
	rhs.val += lhs;
	return rhs;
}
#include &lt;iostream&gt;
int main()
{
	foo&lt;char&gt;::bar&lt;int&gt; tester(5);
	std::cout &lt;&lt; (2 + tester).val;
}
</code></pre>
<p>ausgabe:</p>
<pre><code>Compilation error	 time: 0 memory: 3428 signal:0
prog.cpp:12:44: error: ‘foo&lt;T&gt;::bar’ is not a type
 foo&lt;T&gt;::bar&lt;U&gt; operator+ (int lhs, foo&lt;T&gt;::bar&lt;U&gt; rhs)
                                            ^
prog.cpp:12:47: error: expected ‘,’ or ‘...’ before ‘&lt;’ token
 foo&lt;T&gt;::bar&lt;U&gt; operator+ (int lhs, foo&lt;T&gt;::bar&lt;U&gt; rhs)
                                               ^
prog.cpp:12:54: error: ‘foo&lt;T&gt;::bar&lt;U&gt; operator+(int, int)’ must have an argument of class or enumerated type
 foo&lt;T&gt;::bar&lt;U&gt; operator+ (int lhs, foo&lt;T&gt;::bar&lt;U&gt; rhs)
                                                      ^
prog.cpp: In function ‘int main()’:
prog.cpp:21:18: error: no match for ‘operator+’ (operand types are ‘int’ and ‘foo&lt;char&gt;::bar&lt;int&gt;’)
  std::cout &lt;&lt; (2 + tester).val;
                  ^
</code></pre>
<p>dass bar kein typ sondern ein template ist ist mir auch bewusst, soll der compiler halt bis zur template-argumentenliste weiterlesen...</p>
<p>was kann ich da machen damit das funktioniert?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/321137/klassentemplate-in-klassentemplate</link><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Jul 2026 22:02:49 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/321137.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 25 Oct 2013 09:35:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to klassentemplate in klassentemplate on Fri, 25 Oct 2013 09:35:08 GMT]]></title><description><![CDATA[<p>hallo</p>
<p>ich versuche gerade einen code im stil von dem hier zum laufen zu bekommen (minimalbeispiel):</p>
<pre><code>template&lt;class T&gt;
struct foo
{
	template&lt;class U&gt;
	struct bar
	{
		int val;
		bar(int n) : val(n) {}
	};
};
template&lt;class T, class U&gt;
foo&lt;T&gt;::bar&lt;U&gt; operator+ (int lhs, foo&lt;T&gt;::bar&lt;U&gt; rhs)
{
	rhs.val += lhs;
	return rhs;
}
#include &lt;iostream&gt;
int main()
{
	foo&lt;char&gt;::bar&lt;int&gt; tester(5);
	std::cout &lt;&lt; (2 + tester).val;
}
</code></pre>
<p>ausgabe:</p>
<pre><code>Compilation error	 time: 0 memory: 3428 signal:0
prog.cpp:12:44: error: ‘foo&lt;T&gt;::bar’ is not a type
 foo&lt;T&gt;::bar&lt;U&gt; operator+ (int lhs, foo&lt;T&gt;::bar&lt;U&gt; rhs)
                                            ^
prog.cpp:12:47: error: expected ‘,’ or ‘...’ before ‘&lt;’ token
 foo&lt;T&gt;::bar&lt;U&gt; operator+ (int lhs, foo&lt;T&gt;::bar&lt;U&gt; rhs)
                                               ^
prog.cpp:12:54: error: ‘foo&lt;T&gt;::bar&lt;U&gt; operator+(int, int)’ must have an argument of class or enumerated type
 foo&lt;T&gt;::bar&lt;U&gt; operator+ (int lhs, foo&lt;T&gt;::bar&lt;U&gt; rhs)
                                                      ^
prog.cpp: In function ‘int main()’:
prog.cpp:21:18: error: no match for ‘operator+’ (operand types are ‘int’ and ‘foo&lt;char&gt;::bar&lt;int&gt;’)
  std::cout &lt;&lt; (2 + tester).val;
                  ^
</code></pre>
<p>dass bar kein typ sondern ein template ist ist mir auch bewusst, soll der compiler halt bis zur template-argumentenliste weiterlesen...</p>
<p>was kann ich da machen damit das funktioniert?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2362799</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2362799</guid><dc:creator><![CDATA[template-ception]]></dc:creator><pubDate>Fri, 25 Oct 2013 09:35:08 GMT</pubDate></item><item><title><![CDATA[Reply to klassentemplate in klassentemplate on Fri, 25 Oct 2013 09:43:44 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">template&lt;class T&gt;
template&lt;class U&gt;
typename foo&lt;T&gt;::template bar&lt;U&gt; operator+ (int lhs, typename foo&lt;T&gt;::template bar&lt;U&gt; rhs)
{
...
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2362806</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2362806</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 25 Oct 2013 09:43:44 GMT</pubDate></item><item><title><![CDATA[Reply to klassentemplate in klassentemplate on Fri, 25 Oct 2013 09:48:14 GMT]]></title><description><![CDATA[<p>sowas ähnliches hab ich auch schon versucht, jedoch kommt vom compiler dann immer</p>
<pre><code>prog.cpp:13:34: error: too many template-parameter-lists
 typename foo&lt;T&gt;::template bar&lt;U&gt; operator+ (int lhs, typename foo&lt;T&gt;::template bar&lt;U&gt; rhs)
                                  ^
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2362808</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2362808</guid><dc:creator><![CDATA[template-ception]]></dc:creator><pubDate>Fri, 25 Oct 2013 09:48:14 GMT</pubDate></item><item><title><![CDATA[Reply to klassentemplate in klassentemplate on Fri, 25 Oct 2013 09:53:21 GMT]]></title><description><![CDATA[<p>ah sorry,</p>
<pre><code class="language-cpp">template&lt;class T, class U&gt;
typename foo&lt;T&gt;::template bar&lt;U&gt; operator+ (int lhs, typename foo&lt;T&gt;::template bar&lt;U&gt; rhs)
</code></pre>
<p>nat. nur eine Templateparameterliste. Wir wollen ja kein Membertemplate definieren. Funktionieren wird das trotzdem nicht, weil T so nicht deduziert werden kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2362814</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2362814</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 25 Oct 2013 09:53:21 GMT</pubDate></item><item><title><![CDATA[Reply to klassentemplate in klassentemplate on Fri, 25 Oct 2013 14:41:16 GMT]]></title><description><![CDATA[<p>Wenn du eine Template-Klasse hast, Operatoren <em>immer</em> als friend deklarieren, nie als Template</p>
<pre><code class="language-cpp">template&lt;class T&gt;
struct foo
{
  template&lt;class U&gt;
  struct bar
  {
    int val;
    bar(int n) : val(n) {}

    friend bar operator+ (int lhs, bar rhs)
    {
      rhs.val += lhs;
      return rhs;
    }
  };
};
</code></pre>
<p>Grund dazu ist, dass nur so implizite Konvertierungen greifen (weil die friend-Funktion bei der Instanzierung der Klasse generiert wird). Du hättest auch schreiben können</p>
<pre><code class="language-cpp">friend bar operator+ (bar lhs, bar rhs)
    {
      rhs.val += lhs.val;
      return rhs;
    }
</code></pre>
<p>und du könntest trotzdem &quot;5 + tester&quot; berechnen, weil 5 in bar konvertiert werden kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2362914</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2362914</guid><dc:creator><![CDATA[deducer]]></dc:creator><pubDate>Fri, 25 Oct 2013 14:41:16 GMT</pubDate></item><item><title><![CDATA[Reply to klassentemplate in klassentemplate on Fri, 25 Oct 2013 15:00:00 GMT]]></title><description><![CDATA[<blockquote>
<p>Wenn du eine Template-Klasse hast, Operatoren immer als friend deklarieren, nie als Template</p>
</blockquote>
<p>Das ist Unsinn. Die Regel muss ganz anders formuliert werden:<br />
Überlädt man einen binären Operator für eine Klasse, so bietet es sich manchmal an, diesen als non-member zu überladen, damit implizite Konvertierungen greifen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2362921</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2362921</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Fri, 25 Oct 2013 15:00:00 GMT</pubDate></item><item><title><![CDATA[Reply to klassentemplate in klassentemplate on Fri, 25 Oct 2013 15:42:28 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<blockquote>
<p>Wenn du eine Template-Klasse hast, Operatoren immer als friend deklarieren, nie als Template</p>
</blockquote>
<p>Das ist Unsinn. Die Regel muss ganz anders formuliert werden:<br />
Überlädt man einen binären Operator für eine Klasse, so bietet es sich manchmal an, diesen als non-member zu überladen, damit implizite Konvertierungen greifen.</p>
</blockquote>
<p>Bring mal ein Beispiel, wann das gewünscht ist und es einen impliziten Konstruktor braucht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2362932</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2362932</guid><dc:creator><![CDATA[deducer]]></dc:creator><pubDate>Fri, 25 Oct 2013 15:42:28 GMT</pubDate></item><item><title><![CDATA[Reply to klassentemplate in klassentemplate on Fri, 25 Oct 2013 15:58:15 GMT]]></title><description><![CDATA[<p>Moment, hab' ich deinen Tipp falsch interpretiert?<br />
Ich dachte, du beziehst dich auf das Member-Sein, nicht auf das Deklarieren außerhalb, denn direkt danach hast du ja genau den Grund genannt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<p>Allerdings ist mein ursprünglicher Einwand weiterhin richtig; es sei Unsinn, u.a. da man nie alles standardmäßig als <code>friend</code> deklariert, auch wenn man dann ein wenig Tipparbeit spart. Das ist eine Sünde. Man darf nicht einfach um Tipparbeit zu sparen die Kapselung lockern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2362936</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2362936</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Fri, 25 Oct 2013 15:58:15 GMT</pubDate></item><item><title><![CDATA[Reply to klassentemplate in klassentemplate on Fri, 25 Oct 2013 16:09:23 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Funktionieren wird das trotzdem nicht, weil T so nicht deduziert werden kann.</p>
</blockquote>
<p>Ja, ich verstehe im Übrigen nicht, warum das ein non-deduced context ist:</p>
<p>§14.8.2.5/5 schrieb:</p>
<blockquote>
<p>— The <em>nested-name-specifier</em> of a type that was specified using a <em>qualified-id</em>.</p>
</blockquote>
<p>Könntest du mir das erklären?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2362939</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2362939</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Fri, 25 Oct 2013 16:09:23 GMT</pubDate></item><item><title><![CDATA[Reply to klassentemplate in klassentemplate on Fri, 25 Oct 2013 16:31:05 GMT]]></title><description><![CDATA[<blockquote>
<p>[in Klassentemplates:] Operatoren immer als friend deklarieren</p>
</blockquote>
<p>sollte ziemlich klar sein, ich kann mir nicht vorstellen, wie man da etwas falsch interpretieren könnte.</p>
<p>Arcoth schrieb:</p>
<blockquote>
<p>Allerdings ist mein ursprünglicher Einwand weiterhin richtig; es sei Unsinn, u.a. da man nie alles standardmäßig als <code>friend</code> deklariert, auch wenn man dann ein wenig Tipparbeit spart. Das ist eine Sünde. Man darf nicht einfach um Tipparbeit zu sparen die Kapselung lockern.</p>
</blockquote>
<p>Es wird nirgendwo Kapselung gelockert. Vielleicht formal gesehen lokal in den 2 Zeilen des Operators, aber das ist nicht schlimm. Das hat auch nichts mit Kapselung zu tun.</p>
<p>Und nebenbei bemerkt gibt es (normalerweise, auch hier) keine Alternative. Das hat nichts mit Tipparbeit zu tun.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2362942</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2362942</guid><dc:creator><![CDATA[deducer]]></dc:creator><pubDate>Fri, 25 Oct 2013 16:31:05 GMT</pubDate></item><item><title><![CDATA[Reply to klassentemplate in klassentemplate on Fri, 25 Oct 2013 18:44:52 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>Funktionieren wird das trotzdem nicht, weil T so nicht deduziert werden kann.</p>
</blockquote>
<p>Ja, ich verstehe im Übrigen nicht, warum das ein non-deduced context ist:</p>
<p>§14.8.2.5/5 schrieb:</p>
<blockquote>
<p>— The <em>nested-name-specifier</em> of a type that was specified using a <em>qualified-id</em>.</p>
</blockquote>
<p>Könntest du mir das erklären?</p>
</blockquote>
<p>weil es im Allgemeinen nicht möglich ist</p>
<pre><code class="language-cpp">template &lt;typename T&gt;
struct strange
{
    using type = T*;
};
template &lt;typename T&gt;
struct strange&lt;T*&gt;
{
    using type = T;
};

template &lt;typename T&gt;
void foo(typename strange&lt;T&gt;::type);

...
int* x;
foo(x); // T=int oder T = int** ?
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2362973</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2362973</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 25 Oct 2013 18:44:52 GMT</pubDate></item><item><title><![CDATA[Reply to klassentemplate in klassentemplate on Sat, 26 Oct 2013 06:51:58 GMT]]></title><description><![CDATA[<blockquote>
<p>Vielleicht formal gesehen lokal in den 2 Zeilen des Operators, aber das ist nicht schlimm.</p>
</blockquote>
<p>Doch.</p>
<blockquote>
<p>Und nebenbei bemerkt gibt es (normalerweise, auch hier) keine Alternative.</p>
</blockquote>
<p>Leider sind <em>injected-class-names</em> nicht in inneren Klassen verfügbar... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /></p>
<blockquote>
<p>sollte ziemlich klar sein, ich kann mir nicht vorstellen, wie man da etwas falsch interpretieren könnte.</p>
</blockquote>
<p>Folglich ist deine Begründung dann irgendwie unpassend:</p>
<blockquote>
<p>Wenn du eine Template-Klasse hast, Operatoren immer als friend deklarieren, nie als Template [...]<br />
Grund dazu ist, dass nur so implizite Konvertierungen greifen</p>
</blockquote>
<blockquote>
<p>weil es im Allgemeinen nicht möglich ist</p>
</blockquote>
<p>Das ist mir auch klar. Aber da, wo es nicht möglich ist, könnte man es ill-formed machen, und wo nicht, da nicht. Ist die Entität, auf die die qualified-id sich bezieht, ein UDT innerhalb jener Klasse, ist es eindeutig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2363015</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2363015</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sat, 26 Oct 2013 06:51:58 GMT</pubDate></item></channel></rss>