<?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[temporäre instanz bei default argument vermeiden]]></title><description><![CDATA[<p>ich möchte gern folgendes machen:</p>
<pre><code class="language-cpp">void foo(boost::any const &amp; arg = 0) { ... }
</code></pre>
<p>ich gehe davon aus, dass dieser code für jeden &quot;leeren&quot; aufruf eine temporäre instanz von <a href="http://www.boost.org/doc/libs/1_35_0/boost/any.hpp" rel="nofollow">boost::any</a> erzeugt.</p>
<p>also hab ich es geändert:</p>
<pre><code class="language-cpp">static boost::any const none;
void foo(boost::any const &amp; arg = none) { ... }
</code></pre>
<p>so muss doch keine temporäre instanz mehr erzeugt werden?<br />
was wäre in folgendem fall:</p>
<pre><code class="language-cpp">static boost::any const none;
void foo(boost::any const &amp; arg = none) { ... }

// andere compileunit
class my_class {
public:
    my_class() { foo(); } // ruft foo ohne argument auf    
};

my_class my_object; // globale instanz von my_class
</code></pre>
<p>sehe ich das richtig, dass dieser code undefiniertes verhalten erzeugt, weil foo aufgerufen wird, bevor none sicher erzeugt wurde?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/213254/temporäre-instanz-bei-default-argument-vermeiden</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 10:21:14 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/213254.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 15 May 2008 16:55:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to temporäre instanz bei default argument vermeiden on Thu, 15 May 2008 16:55:25 GMT]]></title><description><![CDATA[<p>ich möchte gern folgendes machen:</p>
<pre><code class="language-cpp">void foo(boost::any const &amp; arg = 0) { ... }
</code></pre>
<p>ich gehe davon aus, dass dieser code für jeden &quot;leeren&quot; aufruf eine temporäre instanz von <a href="http://www.boost.org/doc/libs/1_35_0/boost/any.hpp" rel="nofollow">boost::any</a> erzeugt.</p>
<p>also hab ich es geändert:</p>
<pre><code class="language-cpp">static boost::any const none;
void foo(boost::any const &amp; arg = none) { ... }
</code></pre>
<p>so muss doch keine temporäre instanz mehr erzeugt werden?<br />
was wäre in folgendem fall:</p>
<pre><code class="language-cpp">static boost::any const none;
void foo(boost::any const &amp; arg = none) { ... }

// andere compileunit
class my_class {
public:
    my_class() { foo(); } // ruft foo ohne argument auf    
};

my_class my_object; // globale instanz von my_class
</code></pre>
<p>sehe ich das richtig, dass dieser code undefiniertes verhalten erzeugt, weil foo aufgerufen wird, bevor none sicher erzeugt wurde?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1509888</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1509888</guid><dc:creator><![CDATA[suma dartson]]></dc:creator><pubDate>Thu, 15 May 2008 16:55:25 GMT</pubDate></item><item><title><![CDATA[Reply to temporäre instanz bei default argument vermeiden on Thu, 15 May 2008 17:42:03 GMT]]></title><description><![CDATA[<p>suma dartson schrieb:</p>
<blockquote>
<p>sehe ich das richtig, dass dieser code undefiniertes verhalten erzeugt, weil foo aufgerufen wird, bevor none sicher erzeugt wurde?</p>
</blockquote>
<p>Das hängt davon ab, was foo mit dem Argument anstellt. Ein lvalue kann an eine Referenz gebunden werden, sofern es auf entsprechend reservierten Speicher verweist - es ist nicht zwingend erforderlich, dass das Objekt bereits oder noch lebt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1509911</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1509911</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 15 May 2008 17:42:03 GMT</pubDate></item><item><title><![CDATA[Reply to temporäre instanz bei default argument vermeiden on Thu, 15 May 2008 17:46:49 GMT]]></title><description><![CDATA[<p>Würde sich bei deinem Beispiel nicht Überladung anbieten?</p>
<pre><code class="language-cpp">void foo();
void foo(boost::any const&amp; arg);
</code></pre>
<p>Damit wird bestimmt keine unnötige Kopie erzeugt oder undefiniertes Verhalten ausgelöst. Ausserdem entfällt eine Abfrage wie if (arg == none), du ja weisst, ob ein Argument übergeben wurde.</p>
<p>Gruß<br />
Don06</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1509913</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1509913</guid><dc:creator><![CDATA[Don06]]></dc:creator><pubDate>Thu, 15 May 2008 17:46:49 GMT</pubDate></item><item><title><![CDATA[Reply to temporäre instanz bei default argument vermeiden on Thu, 15 May 2008 17:58:02 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Das hängt davon ab, was foo mit dem Argument anstellt.</p>
</blockquote>
<p>mindestens mal</p>
<pre><code class="language-cpp">if(!arg.empty())
{
   ...
}
</code></pre>
<p>Don06 schrieb:</p>
<blockquote>
<p>Würde sich bei deinem Beispiel nicht Überladung anbieten?</p>
</blockquote>
<p>wenn es nur um die eine stelle ginge, ja. aber es geht um ein interface, wo foo öfter mal überschrieben wird, da möchte ich das lieber mit einer funktion und default-argument machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1509924</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1509924</guid><dc:creator><![CDATA[suma dartson]]></dc:creator><pubDate>Thu, 15 May 2008 17:58:02 GMT</pubDate></item><item><title><![CDATA[Reply to temporäre instanz bei default argument vermeiden on Thu, 15 May 2008 18:11:41 GMT]]></title><description><![CDATA[<p>suma dartson schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>Das hängt davon ab, was foo mit dem Argument anstellt.</p>
</blockquote>
<p>mindestens mal</p>
<pre><code class="language-cpp">if(!arg.empty())
{
   ...
}
</code></pre>
</blockquote>
<p>könnte auch</p>
<pre><code class="language-cpp">if(&amp;arg!=&amp;none&amp;&amp;!arg.empty())
{
   ...
}
</code></pre>
<p>sein, dann besteht kein Problem (nicht, dass ich das für eine gute Lösung halte).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1509939</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1509939</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 15 May 2008 18:11:41 GMT</pubDate></item><item><title><![CDATA[Reply to temporäre instanz bei default argument vermeiden on Thu, 15 May 2008 22:25:07 GMT]]></title><description><![CDATA[<p>Mach es so:</p>
<pre><code class="language-cpp">void foo(boost::any const &amp; arg = boost::any()) { ... }
</code></pre>
<p>Dann wird zwar auch immer ein temporäres Objekt erzeugt, allerdings ist ein &quot;leeres any&quot; sehr billig zu erzeugen &amp; löschen.<br />
Ein &quot;any&quot; mit einem Integer drin kostet dagegen u.U. wesentlich mehr.</p>
<p>Ich weiss nicht ob die aktuelle Boost Version eine Art &quot;small object optimization&quot; bei any verwendet. Wenn das so wäre wäre ein &quot;any&quot; mit einem Integer drinnen vermutlich auch nicht SEHR teuer. Wenn es keine Optimierung gibt ist der Unterschied allerdings sicher dramatisch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1510124</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510124</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Thu, 15 May 2008 22:25:07 GMT</pubDate></item><item><title><![CDATA[Reply to temporäre instanz bei default argument vermeiden on Fri, 16 May 2008 08:16:56 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>Mach es so:</p>
<pre><code class="language-cpp">void foo(boost::any const &amp; arg = boost::any()) { ... }
</code></pre>
</blockquote>
<p>ok, das scheint mir ein guter kompromiss zu sein. ein leeres any kostet wirklich nicht viel.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1510259</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510259</guid><dc:creator><![CDATA[suma dartson]]></dc:creator><pubDate>Fri, 16 May 2008 08:16:56 GMT</pubDate></item></channel></rss>