<?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[Simple TMP Programme]]></title><description><![CDATA[<p>Könnt ihr mal Beispiele für simple TMP Programme zeigen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/310687/simple-tmp-programme</link><generator>RSS for Node</generator><lastBuildDate>Tue, 04 Aug 2026 07:52:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/310687.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 18 Nov 2012 10:27:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Simple TMP Programme on Sun, 18 Nov 2012 10:27:06 GMT]]></title><description><![CDATA[<p>Könnt ihr mal Beispiele für simple TMP Programme zeigen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2271967</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2271967</guid><dc:creator><![CDATA[Programmier-Niveau]]></dc:creator><pubDate>Sun, 18 Nov 2012 10:27:06 GMT</pubDate></item><item><title><![CDATA[Reply to Simple TMP Programme on Sun, 18 Nov 2012 10:38:03 GMT]]></title><description><![CDATA[<p>Kannst du mal eine Suchmaschine nach sowas fragen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2271969</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2271969</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Sun, 18 Nov 2012 10:38:03 GMT</pubDate></item><item><title><![CDATA[Reply to Simple TMP Programme on Sun, 18 Nov 2012 10:50:29 GMT]]></title><description><![CDATA[<p>Kommt nix gescheites, nur sowas.<br />
<a href="http://en.wikibooks.org/wiki/C%2B%2B_Programming/Templates/Template_Meta-Programming#History_of_TMP" rel="nofollow">http://en.wikibooks.org/wiki/C%2B%2B_Programming/Templates/Template_Meta-Programming#History_of_TMP</a></p>
<p>Was für Sachen macht ihr damit?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2271971</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2271971</guid><dc:creator><![CDATA[Programmier-Niveau]]></dc:creator><pubDate>Sun, 18 Nov 2012 10:50:29 GMT</pubDate></item><item><title><![CDATA[Reply to Simple TMP Programme on Sun, 18 Nov 2012 11:11:04 GMT]]></title><description><![CDATA[<p>Programmier-Niveau schrieb:</p>
<blockquote>
<p>Was für Sachen macht ihr damit?</p>
</blockquote>
<p>Mal etwas älteres von mir und camper: <a href="http://www.c-plusplus.net/forum/p2236280#2236280" rel="nofollow">http://www.c-plusplus.net/forum/p2236280#2236280</a><br />
Also eine ROT13-Verschlüsselung von Strings zur Compile-Zeit.</p>
<p>Ist aber eher für die Lolz. Man kann z. B. auch PI zur Compile-Zeit berechnen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2271982</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2271982</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 18 Nov 2012 11:11:04 GMT</pubDate></item><item><title><![CDATA[Reply to Simple TMP Programme on Sun, 18 Nov 2012 11:13:31 GMT]]></title><description><![CDATA[<p>Hier ein weiteres Spaßbeispiel, dieses Mal etwas altes von mir:<br />
<a href="http://www.c-plusplus.net/forum/p2189560#2189560" rel="nofollow">http://www.c-plusplus.net/forum/p2189560#2189560</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2271984</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2271984</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 18 Nov 2012 11:13:31 GMT</pubDate></item><item><title><![CDATA[Reply to Simple TMP Programme on Sun, 18 Nov 2012 12:09:22 GMT]]></title><description><![CDATA[<p>Ganz einfaches Beispiel:</p>
<pre><code class="language-cpp">#include &lt;iterator&gt;
#include &lt;list&gt;
#include &lt;vector&gt;

int main() {
  std::vector&lt;int&gt; v { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Ui, C++11
  std::list  &lt;int&gt; l { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

  auto iv = v.begin();
  auto il = l.begin();

  std::advance(iv, 5); // Ui, TMP
  std::advance(il, 5);
}
</code></pre>
<p>...worin der Compiler in std::advance die Iterator-Kategorie des übergebenen Iterators ausklamüsert und danach auswählt, auf welche Art der Iterator erhöht wird - im Vektor-Fall iv += 5;, im Listen-Fall for(int i = 0; i &lt; 5; ++i) { ++il; }.</p>
<p>Aber seldon, höre ich dich sagen, das ist doch so einfach, kann das wirklich TMP sein?</p>
<p>Albernheiten beiseite, ja, das ist TMP, und es ist ein besseres Anschauungsbeispiel als Fibonacci-Zahlen zur Compilezeit auszurechnen, weil es näher an das herankommt, wofür man TMP wirklich brauchen kann. Wofür will ich den Compiler wirklich ackern lassen? Hauptsächlich, um Entscheidungen im Typsystem zu automatisieren.</p>
<p>Anderes Beispiel: Ich habe mehrere Kindklassen einer gemeinsamen Basisklasse und weiß an einer Stelle, dass ich eine davon zur Laufzeit instanziieren will. Ich will außerdem den Overhead einer Heap-Allokation vermeiden, weil ich mich in einem Bottleneck befinde. Also grabe ich std::aligned_storage aus und mache den Kram mit placement-new von Hand. Was will ich dem Compiler aufdrücken? Der soll mir die Größe des Speicherplatzes, den ich brauche, und das passende Alignment ausrechnen. Und so kommen wir beispielsweise zu</p>
<pre><code class="language-cpp">namespace const_algo {
  template&lt;typename T&gt; constexpr T max(T x, T y) { return x &gt; y ? x : y; }
  template&lt;typename T&gt; constexpr T min(T x, T y) { return x &lt; y ? x : y; }

  namespace impl {
    template&lt;typename T&gt;
    constexpr T lcm_helper(T upper, T lower, T interim) {
      return interim % lower == 0 ? interim : lcm_helper(upper, lower, interim + upper);
    }
  }

  template&lt;typename T&gt;
  constexpr T lcm(T x, T y) {
    return impl::lcm_helper(::const_algo::max(x, y),
                            ::const_algo::min(x, y),
                            ::const_algo::max(x, y));
  }
}

namespace inplace {
  template&lt;typename... types&gt; struct geometry;

  template&lt;typename head_type, typename... tail_types&gt;
  struct geometry&lt;head_type, tail_types...&gt; {
    static std::size_t const space     = const_algo::max( sizeof(head_type), geometry&lt;tail_types...&gt;::space    );
    static std::size_t const alignment = const_algo::lcm(alignof(head_type), geometry&lt;tail_types...&gt;::alignment);
  };

  template&lt;&gt;
  struct geometry&lt;&gt; {
    static std::size_t const space     = 0;
    static std::size_t const alignment = 1;
  };

  ...

  template&lt;typename    base_type,
           typename... possible_types&gt;
  struct factory {
    ...

    typedef geometry&lt;possible_types...&gt;                                   geometry_type;
    typedef typename std::aligned_storage&lt;geometry_type::space,
                                          geometry_type::alignment&gt;::type storage_type;

    storage_type storage_;

    ...
  };
}
</code></pre>
<p>Und wenn du jetzt ganz hart bist, kannst du dir mal ankucken, wie die Leute bei Boost.Spirit die Attribute ihrer Parser zur Compilezeit ausrechnen.</p>
<p>Die Spielereien mit Zahlen und Strings sind ganz nett, und es ist gut, dass so was geht, aber wenn man das ganze ernsthaft betreibt, wirft man die meiste Zeit mit Typen um sich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2271999</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2271999</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Sun, 18 Nov 2012 12:09:22 GMT</pubDate></item><item><title><![CDATA[Reply to Simple TMP Programme on Sun, 18 Nov 2012 12:16:37 GMT]]></title><description><![CDATA[<p>Na, wenn man sich dafür Interessiert dann gibt es zwei Sachen die man sich ansehen sollte:</p>
<ul>
<li><a href="http://www.boost.org/doc/libs/1_52_0/libs/mpl/doc/index.html" rel="nofollow"><strong>Boost.MPL</strong></a></li>
<li><a href="http://www.boost.org/doc/libs/1_52_0/libs/preprocessor/doc/index.html" rel="nofollow"><strong>Boost.PP</strong></a></li>
</ul>
]]></description><link>https://www.c-plusplus.net/forum/post/2272000</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272000</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 18 Nov 2012 12:16:37 GMT</pubDate></item><item><title><![CDATA[Reply to Simple TMP Programme on Sun, 18 Nov 2012 13:02:56 GMT]]></title><description><![CDATA[<p>Boost.Preprocessor ist, wie der Name schon sagt, keine TMP-Bibliothek. Ist aber trotzdem manchmal praktisch, wenn man sich mit alten Compilern herumschlagen muss, die noch keine variadischen Templates kennen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2272019</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272019</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Sun, 18 Nov 2012 13:02:56 GMT</pubDate></item><item><title><![CDATA[Reply to Simple TMP Programme on Sun, 18 Nov 2012 13:03:50 GMT]]></title><description><![CDATA[<p>Darf man bei TMP kein if verwenden, sondern nur ? :</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2272020</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272020</guid><dc:creator><![CDATA[noif]]></dc:creator><pubDate>Sun, 18 Nov 2012 13:03:50 GMT</pubDate></item><item><title><![CDATA[Reply to Simple TMP Programme on Sun, 18 Nov 2012 13:25:08 GMT]]></title><description><![CDATA[<p>Alles, was du in TMP benutzt, wird zur Compilezeit ausgewertet, deswegen herrschen da ziemlich strenge Regeln. If-Blöcke sind da nicht erlaubt, nein. Sie könnten auch nur in stark eingeschränkter Form funktionieren.</p>
<p>Es gibt allerdings Überlegungen, ein static_if (wie in D) einzuführen. Ich sehe nicht, dass das in diesem konkreten Fall sehr sinnvoll wäre, aber in anderen Zusammenhängen könnte das den Code stark vereinfachen. Mal kucken, was daraus wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2272033</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272033</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Sun, 18 Nov 2012 13:25:08 GMT</pubDate></item><item><title><![CDATA[Reply to Simple TMP Programme on Sun, 18 Nov 2012 16:29:19 GMT]]></title><description><![CDATA[<p><a href="http://www.c-plusplus.net/forum/p2220067#2220067" rel="nofollow">http://www.c-plusplus.net/forum/p2220067#2220067</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2272089</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272089</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Sun, 18 Nov 2012 16:29:19 GMT</pubDate></item></channel></rss>