<?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[Warum funktioniert das nicht? (unique_ptr, move konstruktor)]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;memory&gt;
using namespace std;

struct A
{
   A(){}
   A(unique_ptr&lt;A&gt;&amp;&amp; other):ptr(other)
   {

   }
   unique_ptr&lt;A&gt; ptr;
};

int main()
{
    A a;
    A b(std::move(a));
}
</code></pre>
<blockquote>
<p>/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h: In constructor 'A::A(std::unique_ptr&lt;A&gt;&amp;&amp;)':<br />
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:207:7: error: deleted function 'std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;::unique_ptr(const std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;&amp;) [with _Tp = A, _Tp_Deleter = std::default_delete&lt;A&gt;, std::unique_ptr&lt;_Tp, _Tp_Deleter&gt; = std::unique_ptr&lt;A&gt;]'<br />
prog.cpp:7:38: error: used here<br />
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h: In copy constructor 'A::A(const A&amp;)':<br />
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:207:7: error: deleted function 'std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;::unique_ptr(const std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;&amp;) [with _Tp = A, _Tp_Deleter = std::default_delete&lt;A&gt;, std::unique_ptr&lt;_Tp, _Tp_Deleter&gt; = std::unique_ptr&lt;A&gt;]'<br />
prog.cpp:5:1: error: used here<br />
prog.cpp: In function 'int main()':<br />
prog.cpp:17:21: note: synthesized method 'A::A(const A&amp;)' first required here</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/topic/294051/warum-funktioniert-das-nicht-unique_ptr-move-konstruktor</link><generator>RSS for Node</generator><lastBuildDate>Sun, 16 Aug 2026 02:01:57 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/294051.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 16 Oct 2011 17:15:44 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Warum funktioniert das nicht? (unique_ptr, move konstruktor) on Sun, 16 Oct 2011 17:15:44 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;memory&gt;
using namespace std;

struct A
{
   A(){}
   A(unique_ptr&lt;A&gt;&amp;&amp; other):ptr(other)
   {

   }
   unique_ptr&lt;A&gt; ptr;
};

int main()
{
    A a;
    A b(std::move(a));
}
</code></pre>
<blockquote>
<p>/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h: In constructor 'A::A(std::unique_ptr&lt;A&gt;&amp;&amp;)':<br />
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:207:7: error: deleted function 'std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;::unique_ptr(const std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;&amp;) [with _Tp = A, _Tp_Deleter = std::default_delete&lt;A&gt;, std::unique_ptr&lt;_Tp, _Tp_Deleter&gt; = std::unique_ptr&lt;A&gt;]'<br />
prog.cpp:7:38: error: used here<br />
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h: In copy constructor 'A::A(const A&amp;)':<br />
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:207:7: error: deleted function 'std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;::unique_ptr(const std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;&amp;) [with _Tp = A, _Tp_Deleter = std::default_delete&lt;A&gt;, std::unique_ptr&lt;_Tp, _Tp_Deleter&gt; = std::unique_ptr&lt;A&gt;]'<br />
prog.cpp:5:1: error: used here<br />
prog.cpp: In function 'int main()':<br />
prog.cpp:17:21: note: synthesized method 'A::A(const A&amp;)' first required here</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2132201</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132201</guid><dc:creator><![CDATA[*Q* 1]]></dc:creator><pubDate>Sun, 16 Oct 2011 17:15:44 GMT</pubDate></item><item><title><![CDATA[Reply to Warum funktioniert das nicht? (unique_ptr, move konstruktor) on Sun, 16 Oct 2011 17:16:58 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">A(unique_ptr&lt;A&gt;&amp;&amp; other):ptr(other)
</code></pre>
<p>other ist ein LValue. Du musst also schreiben:</p>
<pre><code class="language-cpp">A(unique_ptr&lt;A&gt;&amp;&amp; other):ptr(std::move(other))
</code></pre>
<p>Merke: Sobald ein Ding einen Namen hat, ist es auf jeden Fall ein LValue.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132203</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132203</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Sun, 16 Oct 2011 17:16:58 GMT</pubDate></item><item><title><![CDATA[Reply to Warum funktioniert das nicht? (unique_ptr, move konstruktor) on Sun, 16 Oct 2011 17:19:06 GMT]]></title><description><![CDATA[<p>Hm ich dachte immer &amp;&amp; würde heißen das es ein rvalue ist.<br />
Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132208</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132208</guid><dc:creator><![CDATA[*Q* 1]]></dc:creator><pubDate>Sun, 16 Oct 2011 17:19:06 GMT</pubDate></item><item><title><![CDATA[Reply to Warum funktioniert das nicht? (unique_ptr, move konstruktor) on Sun, 16 Oct 2011 17:22:07 GMT]]></title><description><![CDATA[<p>&amp;&amp; bedeutet &quot;Referenz auf RValue&quot;, nicht, dass der mit &amp;&amp; deklarierte Name selbst ein RValue ist. Du musst unterscheiden zwischen der Referenz und dem Objekt selbst. Das Objekt selbst ist ein RValue, aber du verweißt mit einem LValue darauf.</p>
<p>In der ersten Version von RValue Referenzen waren diese übrigens selbst auch RValues, aber das hat zu Problemen geführt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132211</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132211</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Sun, 16 Oct 2011 17:22:07 GMT</pubDate></item><item><title><![CDATA[Reply to Warum funktioniert das nicht? (unique_ptr, move konstruktor) on Sun, 16 Oct 2011 17:52:30 GMT]]></title><description><![CDATA[<p>Du könntest other ja noch später im Funktionsrumpf verwenden, deshalb kann other nicht einfach &quot;auseinandergenommen&quot; werden. Das musst du dem Compiler dann schon durch std::move() mitteilen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132231</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132231</guid><dc:creator><![CDATA[wxSkip]]></dc:creator><pubDate>Sun, 16 Oct 2011 17:52:30 GMT</pubDate></item><item><title><![CDATA[Reply to Warum funktioniert das nicht? (unique_ptr, move konstruktor) on Sun, 16 Oct 2011 18:48:41 GMT]]></title><description><![CDATA[<p>Hier vielleicht eine einfachere Erklärung.</p>
<p>Wenn du eine Variable hast, gibt es zwei Arten von Typen. Den deklarierten Typen, und den Typen in einem Ausdruck. Mithilfe von decltype bekommst du ersteren. In deinem Fall wäre der deklarierte Typ von other</p>
<pre><code class="language-cpp">std::unique_ptr&lt;A&gt;&amp;&amp;
</code></pre>
<p>Der Typ von other in einem Ausdruck ist aber</p>
<pre><code class="language-cpp">std::unique_ptr&lt;A&gt;&amp;
</code></pre>
<p>Sobald du einen Namen für etwas hast, und diesen irgendwo verwendest, hast du einen T&amp; in der Hand. Das klingt nun aufs erste seltsam, aber mit folgendem Beispiel sollte das klarer werden:</p>
<pre><code class="language-cpp">int a;
a = 666;
</code></pre>
<p>Man kann a etwas zuweisen, denn a hat in jedem Ausdruck den Typ int&amp;. (Es sei denn, dieser wird explizit gecastet.)</p>
<p>Deshalb muss move nochmals auf die RValue Referenz angewandt werden. Damit sagen wir dem Compiler &quot;Wandel mir diesen LValue bitte in einen RValue um&quot;.</p>
<p>move ist eigentlich nur ein Wrapper um ein</p>
<pre><code class="language-cpp">static_cast&lt;T&amp;&amp;&gt;(val);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2132243</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132243</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Sun, 16 Oct 2011 18:48:41 GMT</pubDate></item><item><title><![CDATA[Reply to Warum funktioniert das nicht? (unique_ptr, move konstruktor) on Sun, 16 Oct 2011 19:06:06 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>move ist eigentlich nur ein Wrapper um ein</p>
<pre><code class="language-cpp">static_cast&lt;T&amp;&amp;&gt;(val);
</code></pre>
</blockquote>
<p>Öh... nein?</p>
<pre><code class="language-cpp">template &lt;class T&gt;
typename remove_reference&lt;T&gt;::type&amp;&amp;
move(T&amp;&amp; a)
{
    return a;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2132249</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132249</guid><dc:creator><![CDATA[neinpe]]></dc:creator><pubDate>Sun, 16 Oct 2011 19:06:06 GMT</pubDate></item><item><title><![CDATA[Reply to Warum funktioniert das nicht? (unique_ptr, move konstruktor) on Sun, 16 Oct 2011 19:10:05 GMT]]></title><description><![CDATA[<p>neinpe schrieb:</p>
<blockquote>
<p>314159265358979 schrieb:</p>
<blockquote>
<p>move ist eigentlich nur ein Wrapper um ein</p>
<pre><code class="language-cpp">static_cast&lt;T&amp;&amp;&gt;(val);
</code></pre>
</blockquote>
<p>Öh... nein?</p>
<pre><code class="language-cpp">template &lt;class T&gt;
typename remove_reference&lt;T&gt;::type&amp;&amp;
move(T&amp;&amp; a)
{
    return a;
}
</code></pre>
</blockquote>
<p>Öh... doch! In dieser Funktion ist es nur ein impliziter Cast.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132253</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132253</guid><dc:creator><![CDATA[wxSkip]]></dc:creator><pubDate>Sun, 16 Oct 2011 19:10:05 GMT</pubDate></item></channel></rss>