<?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[[Templates] Wo ist der Fehler?]]></title><description><![CDATA[<pre><code class="language-cpp">template&lt;typename T, T Factor&gt;
struct Reduce
{
	static const T result = 0;
};

template&lt;typename T, T Start, template&lt;class U, U Arg&gt; class Expression&gt;
struct For
{
	static const T result = For&lt;T, Start - 1, template Expression&gt;::result;
};

template&lt;typename T, template&lt;class U, U Arg&gt; class Expression&gt;
struct For&lt;T, 0, template Expression&gt;
{
	static const T result = template Expression&lt;T, Start&gt;::result;
};

template&lt;typename T, T Arg&gt;
struct CallReduce
{
	static const T result = For&lt;T, Arg, Reduce&gt;::result;
};

int main()
{
	CallReduce&lt;unsigned, 2u&gt;::result;
}
</code></pre>
<blockquote>
<p>C:\src\partial.cpp|12|error: template argument 3 is invalid|<br />
C:\src\partial.cpp|18|error: template argument 3 is invalid|<br />
C:\src\partial.cpp|34|instantiated from here|<br />
C:\src\partial.cpp|27|error: <code>CallReduce&lt;unsigned int, 2u&gt;::result' cannot be initialized by a non-constant expression when being declared| C:\\src\\partial.cpp||In function</code>int main()':|<br />
C:\src\partial.cpp|34|warning: statement has no effect|<br />
||=== Build finished: 3 errors, 1 warnings ===|</p>
</blockquote>
<p>ich habe schlicht und einfach keine idee mehr. das weglassen von &quot;template&quot; beim einsetzen als template-argument bringt auch nichts:</p>
<blockquote>
<p>C:\src\partial.cpp|12|error: type/value mismatch at argument 3 in template parameter list for <code>template&lt;class T, T Start, template&lt;class U, U Arg&gt; class Expression&gt; struct For'| C:\\src\\partial.cpp|12|error: expected a template of type \</code>template&lt;class U, U Arg&gt; class Expression', got `template&lt;class U, U Arg&gt; class Expression'|<br />
C:\src\partial.cpp|18|error: type/value mismatch at argument 3 in template parameter list for <code>template&lt;class T, T Start, template&lt;class U, U Arg&gt; class Expression&gt; struct For'| C:\\src\\partial.cpp|18|error: expected a template of type \</code>template&lt;class U, U Arg&gt; class Expression', got `template&lt;class U, U Arg&gt; class Expression'|<br />
C:\src\partial.cpp|34|instantiated from here|<br />
C:\src\partial.cpp|27|error: <code>CallReduce&lt;unsigned int, 2u&gt;::result' cannot be initialized by a non-constant expression when being declared| C:\\src\\partial.cpp||In function</code>int main()':|<br />
C:\src\partial.cpp|34|warning: statement has no effect|<br />
||=== Build finished: 5 errors, 1 warnings ===|</p>
</blockquote>
<p>Compiler ist MinGW der CodeBlocks Version 0.8.2 (G++ 3.4.5?)</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/224917/templates-wo-ist-der-fehler</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 03:29:25 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/224917.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 16 Oct 2008 17:00:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Templates] Wo ist der Fehler? on Thu, 16 Oct 2008 17:00:54 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">template&lt;typename T, T Factor&gt;
struct Reduce
{
	static const T result = 0;
};

template&lt;typename T, T Start, template&lt;class U, U Arg&gt; class Expression&gt;
struct For
{
	static const T result = For&lt;T, Start - 1, template Expression&gt;::result;
};

template&lt;typename T, template&lt;class U, U Arg&gt; class Expression&gt;
struct For&lt;T, 0, template Expression&gt;
{
	static const T result = template Expression&lt;T, Start&gt;::result;
};

template&lt;typename T, T Arg&gt;
struct CallReduce
{
	static const T result = For&lt;T, Arg, Reduce&gt;::result;
};

int main()
{
	CallReduce&lt;unsigned, 2u&gt;::result;
}
</code></pre>
<blockquote>
<p>C:\src\partial.cpp|12|error: template argument 3 is invalid|<br />
C:\src\partial.cpp|18|error: template argument 3 is invalid|<br />
C:\src\partial.cpp|34|instantiated from here|<br />
C:\src\partial.cpp|27|error: <code>CallReduce&lt;unsigned int, 2u&gt;::result' cannot be initialized by a non-constant expression when being declared| C:\\src\\partial.cpp||In function</code>int main()':|<br />
C:\src\partial.cpp|34|warning: statement has no effect|<br />
||=== Build finished: 3 errors, 1 warnings ===|</p>
</blockquote>
<p>ich habe schlicht und einfach keine idee mehr. das weglassen von &quot;template&quot; beim einsetzen als template-argument bringt auch nichts:</p>
<blockquote>
<p>C:\src\partial.cpp|12|error: type/value mismatch at argument 3 in template parameter list for <code>template&lt;class T, T Start, template&lt;class U, U Arg&gt; class Expression&gt; struct For'| C:\\src\\partial.cpp|12|error: expected a template of type \</code>template&lt;class U, U Arg&gt; class Expression', got `template&lt;class U, U Arg&gt; class Expression'|<br />
C:\src\partial.cpp|18|error: type/value mismatch at argument 3 in template parameter list for <code>template&lt;class T, T Start, template&lt;class U, U Arg&gt; class Expression&gt; struct For'| C:\\src\\partial.cpp|18|error: expected a template of type \</code>template&lt;class U, U Arg&gt; class Expression', got `template&lt;class U, U Arg&gt; class Expression'|<br />
C:\src\partial.cpp|34|instantiated from here|<br />
C:\src\partial.cpp|27|error: <code>CallReduce&lt;unsigned int, 2u&gt;::result' cannot be initialized by a non-constant expression when being declared| C:\\src\\partial.cpp||In function</code>int main()':|<br />
C:\src\partial.cpp|34|warning: statement has no effect|<br />
||=== Build finished: 5 errors, 1 warnings ===|</p>
</blockquote>
<p>Compiler ist MinGW der CodeBlocks Version 0.8.2 (G++ 3.4.5?)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1600044</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1600044</guid><dc:creator><![CDATA[opfer der templates]]></dc:creator><pubDate>Thu, 16 Oct 2008 17:00:54 GMT</pubDate></item><item><title><![CDATA[Reply to [Templates] Wo ist der Fehler? on Thu, 16 Oct 2008 17:53:15 GMT]]></title><description><![CDATA[<p>In den Zeilen 12, 17 und 19 hat das Schlüsselwort template nichts zu suchen.<br />
In Zeile 19 Start soll vermutlich 0 sein.<br />
Grundsätzlich liegt das Problem an anderer Stelle: der Typ eines Non-type Arguments einer partiellen Spezialisierung darf nicht abhängig (von einem Parameter dieser Spezialisierung) sein (14.5.5/9 2. Alt.). Weshalb dies Restriktion existiert, ist mir nicht bekannt, ich vermute, dass es zu Mehrdeutigkeiten führen könnte.<br />
Selbstverständlich gibt es einen Ausweg - die allseits beliebte Indirektion: indem wir das Non-Type-Argument in einen Typ stecken, und ein Abbruchprädikat benutzen (hier der Bequemlichkeit wegen als extra Parameter mit Defaultargument)</p>
<pre><code class="language-cpp">template&lt;typename T, T Factor&gt;
struct Reduce
{
    static const T result = 0;
};

template&lt;typename T, T v&gt;
struct value_type
{
    typedef T type;
    static const T value = v;
};

template&lt;typename Start, template&lt;class U, U Arg&gt; class Expression, bool end_recursion = ( Start::value == 0 )&gt;
struct For
{
    static const typename Start::type result = For&lt;value_type&lt;typename Start::type, Start::value - 1&gt;, Expression&gt;::result;
};

template&lt;typename Start, template&lt;class U, U Arg&gt; class Expression&gt;
struct For&lt;Start, Expression, true&gt;
{
    static const typename Start::type result = Expression&lt;typename Start::type, Start::value&gt;::result;
};

template&lt;typename T, T Arg&gt;
struct CallReduce
{
    static const T result = For&lt;value_type&lt;T, Arg&gt;, Reduce&gt;::result;
};

int main()
{
    CallReduce&lt;unsigned, 2u&gt;::result;
}
</code></pre>
<p>Geht vermutlich noch eleganter, das hier nur schnell hingeschrieben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1600073</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1600073</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 16 Oct 2008 17:53:15 GMT</pubDate></item><item><title><![CDATA[Reply to [Templates] Wo ist der Fehler? on Thu, 16 Oct 2008 18:28:42 GMT]]></title><description><![CDATA[<p>danke für den tipp, ich habe es jetzt zum laufen gebracht, allerdings etwas anders:</p>
<pre><code class="language-cpp">template&lt;typename T, T ArgA, T ArgB&gt;
struct NoDiff
{
    static const bool result = (ArgB - ArgA) == 0;
};
</code></pre>
<p>danach habe ich ein extra-argument &quot;bool Quit&quot; in die template-liste von For eingefügt und entsprechend spezialisiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1600099</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1600099</guid><dc:creator><![CDATA[opfer der templates]]></dc:creator><pubDate>Thu, 16 Oct 2008 18:28:42 GMT</pubDate></item></channel></rss>