<?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[Funktion mit variabler Anzahl Argumente aufrufen.]]></title><description><![CDATA[<p>Hi zusammen,</p>
<p>ich bin es schon wieder. Dieses Mal kann ich meine Frage wohl auch etwas kürzer fassen. <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>Ich habe die Funktion execl(), welche eine variable Anzahl an Argumenten akzeptiert und einen Vektor, der alle Argumente enthält. Wie kann ich nun am elegantesten execl() aufrufen, wenn ich zur Compilezeit noch nicht weiß, wieviele Einträge in meinem Vektor stehen.</p>
<p>Die elegantest Möglichkeit, die mir einfällt ist die folgende:</p>
<pre><code class="language-cpp">std::string path, name;
std::vector&lt;std::string&gt; options;

...

switch (options.size())
{
    case 0: execl(path, name, NULL); break;
    case 1: execl(path, name, options[0], NULL); break;
    case 2: execl(path, name, options[0], options[1], NULL); break;
    ...
    case n: execl(path, name, options[0], options[1], ..., options[n], NULL); break;
    default: throw my_exception();
}
</code></pre>
<p>Das Problem hierbei ist, dass ich mir auf eine maximal Anzahl Parameter n festlegen müsste. Das ist zwar gangbar, lieber wäre es mir aber, wenn ich mich da nicht einschränken müsste.</p>
<p>Vielleicht hat einer von euch eine Idee.</p>
<p>Gruß<br />
Paul</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/289924/funktion-mit-variabler-anzahl-argumente-aufrufen</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 21:46:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/289924.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 15 Jul 2011 14:31:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Funktion mit variabler Anzahl Argumente aufrufen. on Fri, 15 Jul 2011 14:31:25 GMT]]></title><description><![CDATA[<p>Hi zusammen,</p>
<p>ich bin es schon wieder. Dieses Mal kann ich meine Frage wohl auch etwas kürzer fassen. <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>Ich habe die Funktion execl(), welche eine variable Anzahl an Argumenten akzeptiert und einen Vektor, der alle Argumente enthält. Wie kann ich nun am elegantesten execl() aufrufen, wenn ich zur Compilezeit noch nicht weiß, wieviele Einträge in meinem Vektor stehen.</p>
<p>Die elegantest Möglichkeit, die mir einfällt ist die folgende:</p>
<pre><code class="language-cpp">std::string path, name;
std::vector&lt;std::string&gt; options;

...

switch (options.size())
{
    case 0: execl(path, name, NULL); break;
    case 1: execl(path, name, options[0], NULL); break;
    case 2: execl(path, name, options[0], options[1], NULL); break;
    ...
    case n: execl(path, name, options[0], options[1], ..., options[n], NULL); break;
    default: throw my_exception();
}
</code></pre>
<p>Das Problem hierbei ist, dass ich mir auf eine maximal Anzahl Parameter n festlegen müsste. Das ist zwar gangbar, lieber wäre es mir aber, wenn ich mich da nicht einschränken müsste.</p>
<p>Vielleicht hat einer von euch eine Idee.</p>
<p>Gruß<br />
Paul</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093193</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093193</guid><dc:creator><![CDATA[_paul_]]></dc:creator><pubDate>Fri, 15 Jul 2011 14:31:25 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion mit variabler Anzahl Argumente aufrufen. on Fri, 15 Jul 2011 14:36:48 GMT]]></title><description><![CDATA[<p>Ist <code>execl()</code> eine Funktion mit Ellipse (...)? Dann kannst du das sowieso gleich vergessen, da das Übergeben von Nicht-PODs (dazu gehört <code>std::string</code> ) an C-VarArg-Funktionen undefiniertes Verhalten ist. Anders siehts aus, wenn es sich um Variadic Templates handelt.</p>
<p>Wie sieht die Signatur von <code>execl()</code> aus?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093195</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093195</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 15 Jul 2011 14:36:48 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion mit variabler Anzahl Argumente aufrufen. on Fri, 15 Jul 2011 14:41:58 GMT]]></title><description><![CDATA[<p>wenn die parameter shcon in einemv ector stehen, wieso übergibst du dann nicht diesen vector?!?</p>
<p>vielleicht noch 2 indexe als bereichs begrenzung (wobei man dann direkt iteratoren übergeben könnte)...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093198</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093198</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Fri, 15 Jul 2011 14:41:58 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion mit variabler Anzahl Argumente aufrufen. on Fri, 15 Jul 2011 14:47:23 GMT]]></title><description><![CDATA[<p>Warum nimmst du nicht execv? Den Vector musst du natürlich trotzdem umkopieren, da Funktionen auf Betriebssystemebene keinen std::string kennen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093203</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093203</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Fri, 15 Jul 2011 14:47:23 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion mit variabler Anzahl Argumente aufrufen. on Fri, 15 Jul 2011 15:05:41 GMT]]></title><description><![CDATA[<p>Danke für die vielen schnellen Antworten. execv sieht nach einer Lösung aus. Das werde ich gleich mal probieren.</p>
<p>Ansonsten muss es natürlich immer options[0].c_str() heißen, das ist richtig. Schande über mein Haupt. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";-)"
      alt="😉"
    /></p>
<p>Die Signaturen der execX-Funktionen stehen zum Beispiel auf <a href="http://linux.die.net/man/3/exec" rel="nofollow">http://linux.die.net/man/3/exec</a>.</p>
<p>Da das Ganze aber ja ein recht allgemeines Problem ist, würde es mich nach wie vor noch interessieren, ob es eine elegantere Möglichkeit gibt, als die von mir unten genannte. Es gibt ja nicht für jede Funktion eine zweite Variante, die auch einen Vektor akzepiert. Oder ist das Problem tatsächlich weniger praxisrelevant, als ich gerade denke?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093213</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093213</guid><dc:creator><![CDATA[_paul_]]></dc:creator><pubDate>Fri, 15 Jul 2011 15:05:41 GMT</pubDate></item></channel></rss>