<?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[warning C4172: returning address of local variable or temporary]]></title><description><![CDATA[<p>Hi,</p>
<p>ich bekomme bei folgendem Template die oben genannte Warnung:</p>
<pre><code class="language-cpp">template&lt;typename T&gt; inline const T&amp; square (const T&amp; value)
{ return (value * value); }

// [...]

int x = 44;
x = square (x);
</code></pre>
<p>Ich habe die Theorie das es an der Referenz liegt die zurück gegeben werden soll. Aber in Scott Meyers &quot;Effektiv C++ Programmieren&quot; wird das doch genau so gemacht in der Einführung bei Thema &quot;templates vs. #define&quot;.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/181327/warning-c4172-returning-address-of-local-variable-or-temporary</link><generator>RSS for Node</generator><lastBuildDate>Tue, 22 Sep 2026 07:13:00 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/181327.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 12 May 2007 14:33:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to warning C4172: returning address of local variable or temporary on Sat, 12 May 2007 14:33:55 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich bekomme bei folgendem Template die oben genannte Warnung:</p>
<pre><code class="language-cpp">template&lt;typename T&gt; inline const T&amp; square (const T&amp; value)
{ return (value * value); }

// [...]

int x = 44;
x = square (x);
</code></pre>
<p>Ich habe die Theorie das es an der Referenz liegt die zurück gegeben werden soll. Aber in Scott Meyers &quot;Effektiv C++ Programmieren&quot; wird das doch genau so gemacht in der Einführung bei Thema &quot;templates vs. #define&quot;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283503</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283503</guid><dc:creator><![CDATA[Template-User]]></dc:creator><pubDate>Sat, 12 May 2007 14:33:55 GMT</pubDate></item><item><title><![CDATA[Reply to warning C4172: returning address of local variable or temporary on Sat, 12 May 2007 14:42:02 GMT]]></title><description><![CDATA[<p>Richtig liegt an der Referenz.</p>
<pre><code class="language-cpp">return (value * value)
</code></pre>
<p>Es wird ein temporäres Objekt angelegt und eine Refernz auf dieses Objekt zurückgegeben, dann wird das Objekt zerstört.<br />
Du hast also eine Referenz auf ein ungültiges Objekt.</p>
<p>Ich weiß nicht, wieso soetwas in deinem Buch steht.<br />
Du solltest die Rückgabe per Wert machen.</p>
<p>Gruß<br />
Don06</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283510</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283510</guid><dc:creator><![CDATA[Don06]]></dc:creator><pubDate>Sat, 12 May 2007 14:42:02 GMT</pubDate></item><item><title><![CDATA[Reply to warning C4172: returning address of local variable or temporary on Sat, 12 May 2007 14:43:15 GMT]]></title><description><![CDATA[<p>Template-User schrieb:</p>
<blockquote>
<p>Hi,</p>
<p>ich bekomme bei folgendem Template die oben genannte Warnung:</p>
<pre><code class="language-cpp">template&lt;typename T&gt; inline const T&amp; square (const T&amp; value)
{ return (value * value); }
</code></pre>
</blockquote>
<p>Das letzte Statement erzeugt eine lokale Kopie (auf dem Stack) und gibt eine Referenz darauf zurück. Nach dem Ende der Funktion wird die Kopie gelöscht (und der Stackpointer zurückgesetzt) und die Referenz zeigt somit ins Leere.</p>
<p>Bau doch mal in den ctors/dem dtor einer Klasse mit der Du das Template instantiierst Testausschriften ein; dann siehtst Du besser was genau passiert.</p>
<p>Grüsse</p>
<p>*this</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283511</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283511</guid><dc:creator><![CDATA[Gast++]]></dc:creator><pubDate>Sat, 12 May 2007 14:43:15 GMT</pubDate></item><item><title><![CDATA[Reply to warning C4172: returning address of local variable or temporary on Sat, 12 May 2007 16:10:25 GMT]]></title><description><![CDATA[<p>Don06 schrieb:</p>
<blockquote>
<p>Richtig liegt an der Referenz.</p>
<pre><code class="language-cpp">return (value * value)
</code></pre>
<p>Es wird ein temporäres Objekt angelegt und eine Refernz auf dieses Objekt zurückgegeben, dann wird das Objekt zerstört.<br />
Du hast also eine Referenz auf ein ungültiges Objekt.</p>
<p>Ich weiß nicht, wieso soetwas in deinem Buch steht.<br />
Du solltest die Rückgabe per Wert machen.</p>
<p>Gruß<br />
Don06</p>
</blockquote>
<p>soweit ich weiss schreibt er in effective c++ eher das gegenteil,<br />
das man das so nicht machen sollte <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/1283586</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283586</guid><dc:creator><![CDATA[inp]]></dc:creator><pubDate>Sat, 12 May 2007 16:10:25 GMT</pubDate></item><item><title><![CDATA[Reply to warning C4172: returning address of local variable or temporary on Sat, 12 May 2007 18:31:20 GMT]]></title><description><![CDATA[<p>Btw *<strong>this</strong> in einer <strong>Methode</strong> als Referenz zurückzugeben ist ganz anders einzuschätzen! Das Objekt (*this) lebt nämlich meist lange genug!</p>
<p>Grüsse</p>
<p>*this</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283666</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283666</guid><dc:creator><![CDATA[Gast++]]></dc:creator><pubDate>Sat, 12 May 2007 18:31:20 GMT</pubDate></item><item><title><![CDATA[Reply to warning C4172: returning address of local variable or temporary on Sun, 13 May 2007 09:02:47 GMT]]></title><description><![CDATA[<p>inp schrieb:</p>
<blockquote>
<p>soweit ich weiss schreibt er in effective c++ eher das gegenteil,<br />
das man das so nicht machen sollte <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>
</blockquote>
<p>Ahh... Das erklärt natürlich einiges. Dachte auch schon: Was ist das denn für ein Buch? Naja, man sollte auch ein Buch lesen und nicht nur Copy&amp;Paste des Codes machen.</p>
<p>Gruß<br />
Don06</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283850</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283850</guid><dc:creator><![CDATA[Don06]]></dc:creator><pubDate>Sun, 13 May 2007 09:02:47 GMT</pubDate></item></channel></rss>