<?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[array mit vorberechneten werten füllen]]></title><description><![CDATA[<p>tag zusammen,</p>
<p>wie kann ich ein array(oder halt vector, std::array etc) mit per TMP vorberechneten werten füllen?</p>
<p>über std::generate kann man ja werte berechnen und das array dann füllen lassen. aber TMP ist ja rekursiv und da fällt mir nicht mals ein ansatz ein...</p>
<p>ich könnte eine klasse(mit einem array als member) schreiben die intern etwas per TMP berechnet und diese werte direkt in den member schreibt. aber auch da bleibt mein denken an laufzeit operationen wie push_back hängen...</p>
<p>hat jemand einen starttipp für mich?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/295372/array-mit-vorberechneten-werten-füllen</link><generator>RSS for Node</generator><lastBuildDate>Sat, 15 Aug 2026 09:38:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/295372.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 12 Nov 2011 17:29:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sat, 12 Nov 2011 17:29:43 GMT]]></title><description><![CDATA[<p>tag zusammen,</p>
<p>wie kann ich ein array(oder halt vector, std::array etc) mit per TMP vorberechneten werten füllen?</p>
<p>über std::generate kann man ja werte berechnen und das array dann füllen lassen. aber TMP ist ja rekursiv und da fällt mir nicht mals ein ansatz ein...</p>
<p>ich könnte eine klasse(mit einem array als member) schreiben die intern etwas per TMP berechnet und diese werte direkt in den member schreibt. aber auch da bleibt mein denken an laufzeit operationen wie push_back hängen...</p>
<p>hat jemand einen starttipp für mich?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143733</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143733</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Sat, 12 Nov 2011 17:29:43 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sat, 12 Nov 2011 17:31:28 GMT]]></title><description><![CDATA[<p>Vielleicht was mit Boost.Fusion oder Boost.MPL. Aber bevor du dir das Leben unnötig schwer machst: Wieso muss es Template-Metaprogrammierung sein?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143734</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143734</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 12 Nov 2011 17:31:28 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sat, 12 Nov 2011 18:01:59 GMT]]></title><description><![CDATA[<p>mh, mir gehts um nen kleinen wettbewerb mit nem kollegen. ein paar fibonacci zahlen berechnen, wers schnellere programm schreibt. (klar ich weis, es gibt diese formel, die die n-te fibonacci zahl berechnet, aber mir gehts um dieses problem)</p>
<p>naja, da kam ich auf die idee, dass man per TMP die zahlen vorberechnet und in dem vector speichert und dann je nach benutzereingabe die n-te fib zahl ausgibt.</p>
<p>und ich hab das gefühl, dass es recht einfach geht, aber ich einfach nicht drauf kommen will Oo</p>
<p>zu deiner frage:<br />
für den praktischen fall müsste es keine TMP sein, aber ich würd gern wissen wie sowas geht, rein aus interesse. geht das auch ohne Boost?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143739</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143739</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Sat, 12 Nov 2011 18:01:59 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sat, 12 Nov 2011 18:08:11 GMT]]></title><description><![CDATA[<p>Google ist dein Freund: <a href="http://www.google.de/search?q=array+temlate+metaprogramming" rel="nofollow">http://www.google.de/search?q=array+temlate+metaprogramming</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143743</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143743</guid><dc:creator><![CDATA[Oberon_0]]></dc:creator><pubDate>Sat, 12 Nov 2011 18:08:11 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sat, 12 Nov 2011 19:45:42 GMT]]></title><description><![CDATA[<p>fib.h:</p>
<pre><code class="language-cpp">#ifndef FIB_H
#define FIB_H

template &lt;int i, int a, int b&gt;
struct FibH
{
  int x;
  FibH&lt;i-1, b, a+b&gt; rest;

  FibH() : x(a) {}
};

template&lt;int a, int b&gt;
struct FibH&lt;0,a,b&gt;
{
  int x;
  FibH() : x(a) {}
};

#endif
</code></pre>
<p>main.cpp:</p>
<pre><code class="language-cpp">#include &quot;fib.h&quot;
#include &lt;iostream&gt;

int main()
{
  const int n = 45;
  FibH&lt;n,0,1&gt; f;

  for(int i = 0; i &lt; n; ++i)
  {
    std::cout &lt;&lt; i &lt;&lt; &quot;:  &quot; &lt;&lt; reinterpret_cast&lt;int*&gt;(&amp;f)[i] &lt;&lt; &quot;\n&quot;;
  }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2143770</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143770</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Sat, 12 Nov 2011 19:45:42 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sat, 12 Nov 2011 20:22:20 GMT]]></title><description><![CDATA[<p>knivil schrieb:</p>
<blockquote>
<pre><code class="language-cpp">...
</code></pre>
</blockquote>
<p>lol.<br />
Nett gemacht. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143773</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143773</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sat, 12 Nov 2011 20:22:20 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sat, 12 Nov 2011 20:53:35 GMT]]></title><description><![CDATA[<p>Prinzipiell von Stackoverflow kopiert: <a href="http://stackoverflow.com/questions/2226291/is-it-possible-to-create-and-initialize-an-array-of-values-using-template-metapr" rel="nofollow">hier</a>. Natuerlich ist der Konstruktor noch haesslich, da vielleicht die Arbeit nur dorthin verschoben wurde. C++11 erlaubt ja:</p>
<pre><code class="language-cpp">template &lt;int n, int a, int b&gt;
class FibH
{
  int x = a;
  ...
};
</code></pre>
<p>So kann auf den Konstruktor verzichtet werden. Obs besser ist und FibH dadurch ein POD wird, keine Ahnung. Ab der n=92 wird es fuer einen 64 Bit Integer kritisch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143775</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143775</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Sat, 12 Nov 2011 20:53:35 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sat, 12 Nov 2011 23:16:26 GMT]]></title><description><![CDATA[<p>Assembleroutput mit VS2010 bei mir:</p>
<pre><code class="language-asm">mov	DWORD PTR _f$[esp+192], 0
	mov	DWORD PTR _f$[esp+196], 1
	mov	DWORD PTR _f$[esp+200], 1
	mov	DWORD PTR _f$[esp+204], 2
	mov	DWORD PTR _f$[esp+208], 3
	mov	DWORD PTR _f$[esp+212], 5
	mov	DWORD PTR _f$[esp+216], 8
	mov	DWORD PTR _f$[esp+220], 13		; 0000000dH
	mov	DWORD PTR _f$[esp+224], 21		; 00000015H
	mov	DWORD PTR _f$[esp+228], 34		; 00000022H
	mov	DWORD PTR _f$[esp+232], 55		; 00000037H
	mov	DWORD PTR _f$[esp+236], 89		; 00000059H
	mov	DWORD PTR _f$[esp+240], 144		; 00000090H
	mov	DWORD PTR _f$[esp+244], 233		; 000000e9H
	mov	DWORD PTR _f$[esp+248], 377		; 00000179H
	mov	DWORD PTR _f$[esp+252], 610		; 00000262H
	mov	DWORD PTR _f$[esp+256], 987		; 000003dbH
	call	??0?$FibH@$0BM@$0GDN@$0KBI@@@QAE@XZ	; FibH&lt;28,1597,2584&gt;::FibH&lt;28,1597,2584&gt;

??0?$FibH@$0BM@$0GDN@$0KBI@@@QAE@XZ PROC		; FibH&lt;28,1597,2584&gt;::FibH&lt;28,1597,2584&gt;, COMDAT
; _this$ = esi

; 9    :   FibH() : x(a) {}

	mov	DWORD PTR [esi], 1597			; 0000063dH
	mov	DWORD PTR [esi+4], 2584			; 00000a18H
	mov	DWORD PTR [esi+8], 4181			; 00001055H
	mov	DWORD PTR [esi+12], 6765		; 00001a6dH
	mov	DWORD PTR [esi+16], 10946		; 00002ac2H
	mov	DWORD PTR [esi+20], 17711		; 0000452fH
	mov	DWORD PTR [esi+24], 28657		; 00006ff1H
	mov	DWORD PTR [esi+28], 46368		; 0000b520H
	mov	DWORD PTR [esi+32], 75025		; 00012511H
	mov	DWORD PTR [esi+36], 121393		; 0001da31H
	mov	DWORD PTR [esi+40], 196418		; 0002ff42H
	mov	DWORD PTR [esi+44], 317811		; 0004d973H
	lea	eax, DWORD PTR [esi+52]
	mov	DWORD PTR [esi+48], 514229		; 0007d8b5H
	call	??0?$FibH@$0P@$0MLCCI@$0BEIKNN@@@QAE@XZ	; FibH&lt;15,832040,1346269&gt;::FibH&lt;15,832040,1346269&gt;
	mov	eax, esi
	ret	0

??0?$FibH@$0P@$0MLCCI@$0BEIKNN@@@QAE@XZ PROC		; FibH&lt;15,832040,1346269&gt;::FibH&lt;15,832040,1346269&gt;, COMDAT
; _this$ = eax

; 9    :   FibH() : x(a) {}

	mov	DWORD PTR [eax], 832040			; 000cb228H
	mov	DWORD PTR [eax+4], 1346269		; 00148addH
	mov	DWORD PTR [eax+8], 2178309		; 00213d05H
	mov	DWORD PTR [eax+12], 3524578		; 0035c7e2H
	mov	DWORD PTR [eax+16], 5702887		; 005704e7H
	mov	DWORD PTR [eax+20], 9227465		; 008cccc9H
	mov	DWORD PTR [eax+24], 14930352		; 00e3d1b0H
	mov	DWORD PTR [eax+28], 24157817		; 01709e79H
	mov	DWORD PTR [eax+32], 39088169		; 02547029H
	mov	DWORD PTR [eax+36], 63245986		; 03c50ea2H
	mov	DWORD PTR [eax+40], 102334155		; 06197ecbH
	mov	DWORD PTR [eax+44], 165580141		; 09de8d6dH
	mov	DWORD PTR [eax+48], 267914296		; 0ff80c38H
	mov	DWORD PTR [eax+52], 433494437		; 19d699a5H
	mov	DWORD PTR [eax+56], 701408733		; 29cea5ddH
	mov	DWORD PTR [eax+60], 1134903170		; 43a53f82H
	ret	0
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2143810</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143810</guid><dc:creator><![CDATA[life]]></dc:creator><pubDate>Sat, 12 Nov 2011 23:16:26 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sun, 13 Nov 2011 10:57:11 GMT]]></title><description><![CDATA[<p>mh also ich hab anfangs die falschen dinge bei google eignegeben und deswegen nix gefunden.</p>
<p>aber jetzt mit dem link von stackoverflow hat das gut geklappt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
und wenn ich das richtig sehe, funktioniert es, dass man für alles berechenbare ein array erstellen kann ?!<br />
man muss nur die &quot;formel&quot; im konstruktor anpassen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143890</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143890</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Sun, 13 Nov 2011 10:57:11 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sun, 13 Nov 2011 14:52:01 GMT]]></title><description><![CDATA[<p>Also <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":-)"
      alt="🙂"
    /></p>
<p>mir war eben langweilig und da hab ich mich mal rangesetzt und das implementiert,<br />
also dass ein richtiges array generiert wird <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":-)"
      alt="🙂"
    /></p>
<p>Der Code ist etwas länger, aber ich hoffe, man kannes trotzdem lesen und verstehen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":-)"
      alt="🙂"
    /></p>
<p>Es werden ein paar C++11 Features verwendet (Variadic Templates &amp; auto).</p>
<p>Ab Zeile 120 kommt der zur Laufzeit relevante Teil, davor ist alles TMP.</p>
<p>Hier der Code:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using std::cout;
using std::endl;

#include &lt;exception&gt;
using std::exception;

#include &lt;stdexcept&gt;
using std::range_error;

#include &lt;boost/lexical_cast.hpp&gt;
using boost::lexical_cast;

// maximum fibonacci number
// must be defined to the compiler using -DMAX_FIB=xxx
#ifndef MAX_FIB
#warning &quot;MAX_FIB undefined! Defaulting to 50.&quot;
#define MAX_FIB 50
#endif

// simple fibonacci function
template &lt;unsigned long long Number&gt;
struct fib
{
    static unsigned long long const value = fib&lt;Number - 2&gt;::value + fib&lt;Number - 1&gt;::value;
};

template &lt;&gt;
struct fib&lt;0&gt;
{
    static unsigned long long const value = 0;
};

template &lt;&gt;
struct fib&lt;1&gt;
{
    static unsigned long long const value = 1;
};

// determine the length of an integer pack
template &lt;unsigned long long... Numbers&gt;
struct length;

template &lt;unsigned long long Head, unsigned long long... Tail&gt;
struct length&lt;Head, Tail...&gt;
{
    static unsigned long long const value = 1 + length&lt;Tail...&gt;::value;
};

template &lt;&gt;
struct length&lt;&gt;
{
    static unsigned long long const value = 0;
};

// a variadic integer pack to integer array converter
template &lt;unsigned long long... Numbers&gt;
struct to_array
{
    static unsigned long long const value[length&lt;Numbers...&gt;::value];
};

template &lt;unsigned long long... Numbers&gt;
unsigned long long const to_array&lt;Numbers...&gt;::value[] = { Numbers... };

// a number list
template &lt;unsigned long long... Numbers&gt;
struct list;

template &lt;unsigned long long Head, unsigned long long... Tail&gt;
struct list&lt;Head, Tail...&gt;
{
    static unsigned long long const head = Head;
    typedef list&lt;Tail...&gt; tail;
};

template &lt;&gt;
struct list&lt;&gt;
{
};

// append to a number list
template &lt;typename List, unsigned long long Value&gt;
struct append;

template &lt;unsigned long long Value&gt;
struct append&lt;list&lt;&gt;, Value&gt;
{
    typedef list&lt;Value&gt; type;
};

template &lt;unsigned long long... Numbers, unsigned long long Value&gt;
struct append&lt;list&lt;Numbers...&gt;, Value&gt;
{
    typedef list&lt;Numbers..., Value&gt; type;
};

// generate number list using fib function
template &lt;unsigned long long Maximum&gt;
struct fib_list
{
    typedef typename append&lt;typename fib_list&lt;Maximum - 1&gt;::type, fib&lt;Maximum&gt;::value&gt;::type type;
};

template &lt;&gt;
struct fib_list&lt;0&gt;
{
    typedef list&lt;fib&lt;0&gt;::value&gt; type;
};

// generate an array from a list
template &lt;typename List&gt;
struct array_from_list;

template &lt;unsigned long long... Numbers&gt;
struct array_from_list&lt;list&lt;Numbers...&gt;&gt;
{
    typedef to_array&lt;Numbers...&gt; type;
};

// our precalculated fibonacci numbers
typedef typename array_from_list&lt;typename fib_list&lt;MAX_FIB&gt;::type&gt;::type fibonacci_array;
unsigned long long const * const fibonacci = fibonacci_array::value;

inline void show_usage(char const * const program)
{
    cout &lt;&lt; &quot;Usage: &quot; &lt;&lt; program &lt;&lt; &quot; [number]&quot; &lt;&lt; endl;
}

int main(int argc, char ** argv)
{
    if (argc != 2)
    {
        cout &lt;&lt; &quot;Wrong number of parameters!\n&quot;;
        show_usage(*argv);
    }

    try
    {
        auto number = lexical_cast&lt;unsigned long long&gt;(argv[1]);
        if (number &gt; MAX_FIB)
            throw range_error(&quot;The given number is too big!&quot;);

        cout &lt;&lt; fibonacci[number] &lt;&lt; endl;
    }
    catch (exception const &amp; ex)
    {
        cout &lt;&lt; &quot;An exception occured: &quot; &lt;&lt; ex.what() &lt;&lt; &quot;\n&quot;;
        show_usage(*argv);
    }

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2143964</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143964</guid><dc:creator><![CDATA[DrakoXP]]></dc:creator><pubDate>Sun, 13 Nov 2011 14:52:01 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sun, 13 Nov 2011 15:08:58 GMT]]></title><description><![CDATA[<p>Das Ganze lässt sich auch ohne reinterpret_cast schreiben. Dann ist auch keine dynamische Initialisierung erforderlich.</p>
<pre><code class="language-cpp">#include &lt;type_traits&gt;
template &lt;std::size_t i&gt; struct fib : std::integral_constant&lt;unsigned long long, fib&lt;i-1&gt;::value + fib&lt;i-2&gt;::value&gt; {};
template &lt;&gt; struct fib&lt;0&gt; : std::integral_constant&lt;unsigned long long, 0&gt; {};
template &lt;&gt; struct fib&lt;1&gt; : std::integral_constant&lt;unsigned long long, 1&gt; {};

template &lt;typename T&gt; struct identity { typedef T type; };
template &lt;std::size_t... i&gt; struct indexes : identity&lt;indexes&lt;i...&gt;&gt; {};
template &lt;typename T&gt; struct append;
template &lt;std::size_t... i&gt; struct append&lt;indexes&lt;i...&gt;&gt; : identity&lt;indexes&lt;0, ( i + 1 )...&gt;&gt; {};
template &lt;std::size_t N&gt; struct make_indexes : append&lt;typename make_indexes&lt;N-1&gt;::type&gt; {};
template &lt;&gt; struct make_indexes&lt;0&gt; : identity&lt;indexes&lt;&gt;&gt; {};

template &lt;template &lt;std::size_t&gt; class func, std::size_t a, std::size_t b, typename = typename make_indexes&lt;b-a+1&gt;::type&gt;
struct func_data;
template &lt;template &lt;std::size_t&gt; class func, std::size_t a, std::size_t b, std::size_t... i&gt;
struct func_data&lt;func, a, b, indexes&lt;i...&gt;&gt;
{
    static const decltype(func&lt;a&gt;::value) data[b-a+1];
};

template &lt;template &lt;std::size_t&gt; class func, std::size_t a, std::size_t b, std::size_t... i&gt;
const decltype(func&lt;a&gt;::value) func_data&lt;func, a, b, indexes&lt;i...&gt;&gt;::data[b-a+1] = { func&lt;a+i&gt;::value... };
/*
template &lt;template &lt;std::size_t&gt; class func, std::size_t a, std::size_t b, std::size_t... i&gt;
struct func_data&lt;func, a, b, indexes&lt;i...&gt;&gt;
{
    static constexpr decltype(func&lt;a&gt;::value) data[] = { func&lt;a+i&gt;::value... };
};
*/
#include &lt;iostream&gt;

int main()
{
  const int n = 45;
  func_data&lt;fib, 0, 44&gt; f;

  for(int i = 0; i &lt; n; ++i)
  {
    std::cout &lt;&lt; i &lt;&lt; &quot;:  &quot; &lt;&lt; f.data[i] &lt;&lt; &quot;\n&quot;;  }
}
</code></pre>
<p>Edit: ok, Drako war etwas schneller.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143965</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143965</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sun, 13 Nov 2011 15:08:58 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sun, 13 Nov 2011 15:14:31 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a>:</p>
<p>wenn ich mir deinen Code so anschaue, muss ich sagen,<br />
dass ich ein paar Sachen bei mir hätte besser machen können :p</p>
<p>Vor allem die Arrayerstellung mit dem Pattern &quot;func&lt;a+i&gt;::value...&quot; war mir vorhin irgendwie entfallen :S</p>
<p>Aber eine Frage zu deinem Code habe ich noch^^</p>
<pre><code>const decltype(func&lt;a&gt;::value) func_data&lt;func, a, b, indexes&lt;i...&gt;&gt;::data[b-a+1] = { func&lt;a+i&gt;::value... };
</code></pre>
<p>Ist das const am Anfang wirklich nötig?<br />
Ich bin mir eben nicht sicher, ob nicht decltype das schon enthalten müsste.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143970</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143970</guid><dc:creator><![CDATA[DrakoXP]]></dc:creator><pubDate>Sun, 13 Nov 2011 15:14:31 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sun, 13 Nov 2011 15:33:17 GMT]]></title><description><![CDATA[<p>DrakoXP schrieb:</p>
<blockquote>
<pre><code>const decltype(func&lt;a&gt;::value) func_data&lt;func, a, b, indexes&lt;i...&gt;&gt;::data[b-a+1] = { func&lt;a+i&gt;::value... };
</code></pre>
<p>Ist das const am Anfang wirklich nötig?</p>
</blockquote>
<p>Erforderlich ist es sicher nicht (sofern man konsequent bleibt). Das ist hier einfach Gewohnheit. Eigentlich sollte es ja ohnehin constexpr sein, aber g++ unterstützt die Aggregatinitialisierung innerhalb der Klassendefinition noch nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143974</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143974</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sun, 13 Nov 2011 15:33:17 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Sun, 13 Nov 2011 17:03:46 GMT]]></title><description><![CDATA[<p>Man kann das ganze auch ohne Verwendung eine fibonacci-Metafunktion schreiben:</p>
<pre><code class="language-cpp">#include &lt;type_traits&gt;

// index-Erstellung mit logarithmischer Komplexität
template &lt;typename T&gt; struct identity { typedef T type; };
template &lt;std::size_t... i&gt; struct indexes
    : identity&lt;indexes&lt;i...&gt;&gt; {};
template &lt;typename... T&gt; struct concat
    : concat&lt;typename T::type...&gt; {};
template &lt;std::size_t... i, std::size_t... j, typename... T&gt; struct concat&lt;indexes&lt;i...&gt;, indexes&lt;j...&gt;, T...&gt;
    : concat&lt;indexes&lt;i..., (sizeof... i + j)...&gt;, T...&gt; {};
template &lt;typename T&gt; struct concat&lt;T&gt;
    : T {};
template &lt;typename T&gt; struct twice
    : concat&lt;T, T&gt; {};
template &lt;std::size_t N&gt; struct make_indexes
    : concat&lt;twice&lt;make_indexes&lt;N/2&gt;&gt;, make_indexes&lt;N%2&gt;&gt; {};
template &lt;&gt; struct make_indexes&lt;0&gt;
    : indexes&lt;&gt; {};
template &lt;&gt; struct make_indexes&lt;1&gt;
    : indexes&lt;0&gt; {};

template &lt;std::size_t N, typename = typename make_indexes&lt;N-2&gt;::type&gt;
struct fib_data;
template &lt;std::size_t N, std::size_t... i&gt;
struct fib_data&lt;N, indexes&lt;i...&gt;&gt;
{
    static const unsigned long long data[N];
};

template &lt;std::size_t N, std::size_t... i&gt;
const unsigned long long fib_data&lt;N, indexes&lt;i...&gt;&gt;::data[N] = { 0, 1, ( data[i] + data[i+1] )... };

#include &lt;iostream&gt;

int main()
{
  const int n = 94;
  fib_data&lt;n&gt; f;

  for(int i = 0; i &lt; n; ++i)
  {
    std::cout &lt;&lt; i &lt;&lt; &quot;:  &quot; &lt;&lt; f.data[i] &lt;&lt; &quot;\n&quot;;
  }
}
</code></pre>
<p>Sieht man von dem Teil davor ab, dürfte diese Form der Initialisierung auch für relative Anfänger ohne TMP-Kentnisse lesbar sein. Allerdings führt das zumindest im Moment bei g++ zu dynamischer Initialisierung, weil die Verwendung von bereits initialisierten Subobjekten in konstanten Ausdrücken bisher nicht implementiert ist (das gleiche Problem hat man auch, wenn man einen constexpr Konstruktor schreiben will).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2144007</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2144007</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sun, 13 Nov 2011 17:03:46 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Thu, 17 Nov 2011 08:31:01 GMT]]></title><description><![CDATA[<p>Mir faellt gerade auf, dass dieser Code mit den ganze ... eigentlich wie Scheme-Makros ausschaut.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2145509</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2145509</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Thu, 17 Nov 2011 08:31:01 GMT</pubDate></item><item><title><![CDATA[Reply to array mit vorberechneten werten füllen on Thu, 17 Nov 2011 09:18:06 GMT]]></title><description><![CDATA[<p>Ich hatte da eigentlich an eine Funktion gedacht, wo drin das Array per static deklariert wird. Aber sonst sieht das auch fast genauso aus, wie bei Euch:</p>
<pre><code class="language-cpp">#include &lt;type_traits&gt;

template&lt;class T&gt; struct identity {typedef T type;};

template&lt;class...T&gt; struct pack {};

template&lt;int Beg, int End, int...Tail&gt;
struct index_pack_type
  : index_pack_type&lt;Beg,End-1,End-1,Tail...&gt; {};
template&lt;int BE, int...Tail&gt;
struct index_pack_type&lt;BE,BE,Tail...&gt;
  : identity&lt;pack&lt;std::integral_constant&lt;int,Tail&gt;...&gt; &gt; {};

template&lt;int B, int E&gt;
typename index_pack_type&lt;B,E&gt;::type make_index_pack() {
  return typename index_pack_type&lt;B,E&gt;::type();
}

template&lt;class ValueType, template&lt;class&gt; class MetaFunc, class...MetaArgs&gt;
inline ValueType const* lookuptable(pack&lt;MetaArgs...&gt;) {
  static const ValueType data[] = {MetaFunc&lt;MetaArgs&gt;::value...};
  return data;
}

#include &lt;iostream&gt;

template&lt;int I&gt; struct fib_c : std::integral_constant&lt;int,
  fib_c&lt;I-1&gt;::value + fib_c&lt;I-2&gt;::value
&gt;{};
template&lt;&gt; struct fib_c&lt;0&gt; : std::integral_constant&lt;int,0&gt;{};
template&lt;&gt; struct fib_c&lt;1&gt; : std::integral_constant&lt;int,1&gt;{};

template&lt;class MetaArg&gt; struct fib : fib_c&lt;MetaArg::value&gt;{};

int main() {
  const int N = 10;
  const int* fibtab = lookuptable&lt;int,fib&gt;(make_index_pack&lt;0,N&gt;());
  for (int i=0; i&lt;N; ++i) {
    std::cout &lt;&lt; fibtab[i] &lt;&lt; std::endl;
  }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2145523</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2145523</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Thu, 17 Nov 2011 09:18:06 GMT</pubDate></item></channel></rss>