<?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[Frage zu Konstruktor und .swap()]]></title><description><![CDATA[<p>Hallo,<br />
ich wollte mir eine kleine Funktion schreiben, die einen Container der stl leert (und auch allen reservierten Speicher freigibt). Mein Ansatz sieht so aus. Allerdings verstehe ich eine Sache nicht:</p>
<pre><code class="language-cpp">template&lt;typename T&gt; inline void container_clear(T&amp; container)
{
    //container.swap(T());  //funktioniert nicht
    (T()).swap(container);  //funktioniert
}
</code></pre>
<p>Warum funktioniert container.swap(T()); nicht?</p>
<pre><code>In function void container_clear(T&amp;) [with T = std::vector&lt;int, std::allocator&lt;int&gt; &gt;]
error: no matching function for call to std::vector&lt;int, std::allocator&lt;int&gt; &gt;::swap(std::vector&lt;int, std::allocator&lt;int&gt; &gt;)
note: candidates are: void std::vector&lt;_Tp, _Alloc&gt;::swap(std::vector&lt;_Tp, _Alloc&gt;&amp;) [with _Tp = int, _Alloc = std::allocator&lt;int&gt;]
</code></pre>
<p>Aus der Fehlermeldung kann ich den Grund nicht erkennen.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/252830/frage-zu-konstruktor-und-swap</link><generator>RSS for Node</generator><lastBuildDate>Mon, 14 Sep 2026 04:54:07 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/252830.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 25 Oct 2009 10:51:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zu Konstruktor und .swap() on Sun, 25 Oct 2009 10:51:27 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich wollte mir eine kleine Funktion schreiben, die einen Container der stl leert (und auch allen reservierten Speicher freigibt). Mein Ansatz sieht so aus. Allerdings verstehe ich eine Sache nicht:</p>
<pre><code class="language-cpp">template&lt;typename T&gt; inline void container_clear(T&amp; container)
{
    //container.swap(T());  //funktioniert nicht
    (T()).swap(container);  //funktioniert
}
</code></pre>
<p>Warum funktioniert container.swap(T()); nicht?</p>
<pre><code>In function void container_clear(T&amp;) [with T = std::vector&lt;int, std::allocator&lt;int&gt; &gt;]
error: no matching function for call to std::vector&lt;int, std::allocator&lt;int&gt; &gt;::swap(std::vector&lt;int, std::allocator&lt;int&gt; &gt;)
note: candidates are: void std::vector&lt;_Tp, _Alloc&gt;::swap(std::vector&lt;_Tp, _Alloc&gt;&amp;) [with _Tp = int, _Alloc = std::allocator&lt;int&gt;]
</code></pre>
<p>Aus der Fehlermeldung kann ich den Grund nicht erkennen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1797869</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1797869</guid><dc:creator><![CDATA[TrekC]]></dc:creator><pubDate>Sun, 25 Oct 2009 10:51:27 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Konstruktor und .swap() on Sun, 25 Oct 2009 11:18:54 GMT]]></title><description><![CDATA[<p>TrekC schrieb:</p>
<blockquote>
<p>Warum funktioniert container.swap(T()); nicht?</p>
</blockquote>
<p>Elementfunktionen können auf temporären Objekten aufgerufen werden.<br />
Du kannst aber temporäre Objekte nicht an eine Funktion übergeben, die das Objekt verändern will, indem sie eine &quot;nicht-const-Referenz&quot; entgegen nimmt.</p>
<p>(Mit &quot;temporär&quot; sind hier namenlose Objekte wie T() gemeint)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1797878</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1797878</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Sun, 25 Oct 2009 11:18:54 GMT</pubDate></item></channel></rss>