<?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[Konstant variable Anzahl von Parametern]]></title><description><![CDATA[<p>Hallo,<br />
ist es möglich, einer Funktion eine von einer Konstanten abhängigen Anzahl von Parametern zu geben?<br />
Zum Beispiel bei einer Klasse wie std::array, die N Elemente vom Typ T enthält. Diese Klasse sollte dann einen Konstruktor erhalten, der genau N Argumente vom Typ T annimmt.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/312768/konstant-variable-anzahl-von-parametern</link><generator>RSS for Node</generator><lastBuildDate>Sun, 02 Aug 2026 19:36:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/312768.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 13 Jan 2013 23:35:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Konstant variable Anzahl von Parametern on Sun, 13 Jan 2013 23:35:07 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ist es möglich, einer Funktion eine von einer Konstanten abhängigen Anzahl von Parametern zu geben?<br />
Zum Beispiel bei einer Klasse wie std::array, die N Elemente vom Typ T enthält. Diese Klasse sollte dann einen Konstruktor erhalten, der genau N Argumente vom Typ T annimmt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2289410</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2289410</guid><dc:creator><![CDATA[Wurstinator]]></dc:creator><pubDate>Sun, 13 Jan 2013 23:35:07 GMT</pubDate></item><item><title><![CDATA[Reply to Konstant variable Anzahl von Parametern on Mon, 14 Jan 2013 00:10:09 GMT]]></title><description><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Variadic_template" rel="nofollow">http://en.wikipedia.org/wiki/Variadic_template</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2289415</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2289415</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Mon, 14 Jan 2013 00:10:09 GMT</pubDate></item><item><title><![CDATA[Reply to Konstant variable Anzahl von Parametern on Mon, 14 Jan 2013 00:41:55 GMT]]></title><description><![CDATA[<p>z.B. so</p>
<pre><code class="language-cpp">#include &lt;utility&gt;
#include &lt;array&gt;
#include &lt;type_traits&gt;

template &lt;typename... T&gt; struct all_same;
template &lt;&gt; struct all_same&lt;&gt;
    : std::true_type {};
template &lt;typename T, typename... U&gt; struct all_same&lt;T, U...&gt;
    : std::is_same&lt;all_same&lt;T, U...&gt;, all_same&lt;U..., T&gt;&gt; {};

template &lt;typename T, int N&gt;
struct my_array
{
//    template &lt;typename... U, typename std::enable_if&lt;sizeof...(U)==N
//        &amp;&amp; all_same&lt;std:.true_type, typename std::is_convertible&lt;U,T&gt;::type...&gt;::value, int&gt;::type = 0&gt;
    template &lt;typename... U&gt;
    my_array(U&amp;&amp;... args)
        : data{ std::forward&lt;U&gt;( args ) ... }
    {
        static_assert( sizeof...(U) == N, &quot;falsche Parameteranzahl&quot; );
        static_assert( all_same&lt;std::true_type, typename std::is_convertible&lt;U,T&gt;::type...&gt;::value, &quot;inkompatibler Parameter&quot; );
    }
    T data[N];
};
</code></pre>
<p>Man kann auch eine entsprechende Typliste im voraus erstellen und als Parameterpack verwenden</p>
<pre><code class="language-cpp">#include &lt;utility&gt;
#include &lt;array&gt;
#include &lt;type_traits&gt;

template &lt;typename... T&gt; struct type_list;

template &lt;typename T, int N, typename = type_list&lt;&gt;&gt; struct repeat_types;
template &lt;typename T, int N, typename... U&gt;
struct repeat_types&lt;T, N, type_list&lt;U...&gt;&gt;
    : repeat_types&lt;T, N-1, type_list&lt;T, U...&gt;&gt; {};
template &lt;typename T, typename... U&gt;
struct repeat_types&lt;T, 0, type_list&lt;U...&gt;&gt; {
    typedef type_list&lt;U...&gt; type;
};

template &lt;typename T, int N, typename = typename repeat_types&lt;T, N&gt;::type&gt;
struct my_array;

template &lt;typename T, int N, typename... U&gt;
struct my_array&lt;T, N, type_list&lt;U...&gt;&gt;
{
    my_array(const U&amp;... args)
        : data{ args... }
    {}

    T data[N];
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2289416</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2289416</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 14 Jan 2013 00:41:55 GMT</pubDate></item><item><title><![CDATA[Reply to Konstant variable Anzahl von Parametern on Mon, 14 Jan 2013 15:16:23 GMT]]></title><description><![CDATA[<p>Danke, camper, ich habe die erste Variante übernommen <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2289600</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2289600</guid><dc:creator><![CDATA[Wurstinator]]></dc:creator><pubDate>Mon, 14 Jan 2013 15:16:23 GMT</pubDate></item></channel></rss>