<?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[Kurze Frage zum shared_ptr Konstruktor]]></title><description><![CDATA[<p>Hallo, ich möchte einen vector aus shared_ptr erstellen, die auf std::mutex zeigen. So funktioniert es:</p>
<pre><code>std::vector&lt;std::shared_ptr&lt;std::mutex&gt;&gt; v;
v.push_back(std::shared_ptr&lt;std::mutex&gt;(new std::mutex()));
</code></pre>
<p>Aber so bekomme ich eine Fehlermeldung:</p>
<pre><code>std::vector&lt;std::shared_ptr&lt;std::mutex&gt;&gt; v;
v.push_back(new std::mutex());
</code></pre>
<pre><code>error C2664: 'void std::vector&lt;_Ty&gt;::push_back(std::shared_ptr&lt;std::mutex&gt; &amp;&amp;)' : cannot convert parameter 1 from 'std::mutex *' to 'std::shared_ptr&lt;_Ty&gt; &amp;&amp;'
1&gt;          with
1&gt;          [
1&gt;              _Ty=std::shared_ptr&lt;std::mutex&gt;
1&gt;          ]
1&gt;          and
1&gt;          [
1&gt;              _Ty=std::mutex
1&gt;          ]
1&gt;          Reason: cannot convert from 'std::mutex *' to 'std::shared_ptr&lt;_Ty&gt;'
1&gt;          with
1&gt;          [
1&gt;              _Ty=std::mutex
1&gt;          ]
1&gt;          Constructor for class 'std::shared_ptr&lt;_Ty&gt;' is declared 'explicit'
1&gt;          with
1&gt;          [
1&gt;              _Ty=std::mutex
1&gt;          ]
</code></pre>
<p>Sollte push_back nicht ein shared_ptr über folgenden Konstruktor erstellen?</p>
<pre><code>template&lt; class Y &gt; 
explicit shared_ptr( Y* ptr );
Quelle: http://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/318270/kurze-frage-zum-shared_ptr-konstruktor</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Jul 2026 10:51:40 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/318270.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 06 Jul 2013 14:07:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sat, 06 Jul 2013 14:07:33 GMT]]></title><description><![CDATA[<p>Hallo, ich möchte einen vector aus shared_ptr erstellen, die auf std::mutex zeigen. So funktioniert es:</p>
<pre><code>std::vector&lt;std::shared_ptr&lt;std::mutex&gt;&gt; v;
v.push_back(std::shared_ptr&lt;std::mutex&gt;(new std::mutex()));
</code></pre>
<p>Aber so bekomme ich eine Fehlermeldung:</p>
<pre><code>std::vector&lt;std::shared_ptr&lt;std::mutex&gt;&gt; v;
v.push_back(new std::mutex());
</code></pre>
<pre><code>error C2664: 'void std::vector&lt;_Ty&gt;::push_back(std::shared_ptr&lt;std::mutex&gt; &amp;&amp;)' : cannot convert parameter 1 from 'std::mutex *' to 'std::shared_ptr&lt;_Ty&gt; &amp;&amp;'
1&gt;          with
1&gt;          [
1&gt;              _Ty=std::shared_ptr&lt;std::mutex&gt;
1&gt;          ]
1&gt;          and
1&gt;          [
1&gt;              _Ty=std::mutex
1&gt;          ]
1&gt;          Reason: cannot convert from 'std::mutex *' to 'std::shared_ptr&lt;_Ty&gt;'
1&gt;          with
1&gt;          [
1&gt;              _Ty=std::mutex
1&gt;          ]
1&gt;          Constructor for class 'std::shared_ptr&lt;_Ty&gt;' is declared 'explicit'
1&gt;          with
1&gt;          [
1&gt;              _Ty=std::mutex
1&gt;          ]
</code></pre>
<p>Sollte push_back nicht ein shared_ptr über folgenden Konstruktor erstellen?</p>
<pre><code>template&lt; class Y &gt; 
explicit shared_ptr( Y* ptr );
Quelle: http://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2337033</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337033</guid><dc:creator><![CDATA[SharedPTR]]></dc:creator><pubDate>Sat, 06 Jul 2013 14:07:33 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sat, 06 Jul 2013 14:12:28 GMT]]></title><description><![CDATA[<p>Der Konstruktor von shared_ptr ist explicit.<br />
Das bedeutet eben, dass du diesen Fehler hast, da eine implizite Typumwandlung unterbunden wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337035</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337035</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 06 Jul 2013 14:12:28 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sat, 06 Jul 2013 14:23:29 GMT]]></title><description><![CDATA[<p>Benutz emplace_back, oder noch besser: make_shared.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337037</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337037</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Sat, 06 Jul 2013 14:23:29 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sat, 06 Jul 2013 14:34:17 GMT]]></title><description><![CDATA[<p>Siehe dir bei Gelegenheit auch mal die Pointer-Container von Boost an, speziell<a href="http://www.boost.org/libs/ptr_container/doc/ptr_vector.html" rel="nofollow"> <code>ptr_vector</code> </a>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337038</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337038</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sat, 06 Jul 2013 14:34:17 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sat, 06 Jul 2013 14:36:26 GMT]]></title><description><![CDATA[<p>Ah, make_shared klingt super! Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337039</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337039</guid><dc:creator><![CDATA[SharedPTR]]></dc:creator><pubDate>Sat, 06 Jul 2013 14:36:26 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sat, 06 Jul 2013 14:53:51 GMT]]></title><description><![CDATA[<p>Zur Not auch ein temporäres shared_ptr Objekt.</p>
<pre><code>// blabla
{
    std::shared_ptr&lt;std::mutex&gt; tmp(new std::mutex());
    vec.push_back(tmp);
}
// blabla
</code></pre>
<p>Aber nicht (!!!) das hier:</p>
<pre><code>// blabla
{
    vec.push_back(std::shared_ptr&lt;std::mutex&gt;(new std::mutex()));
}
// blabla
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2337041</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337041</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Sat, 06 Jul 2013 14:53:51 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sat, 06 Jul 2013 15:04:29 GMT]]></title><description><![CDATA[<p>Warum?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337043</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337043</guid><dc:creator><![CDATA[ölälöälöä]]></dc:creator><pubDate>Sat, 06 Jul 2013 15:04:29 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sat, 06 Jul 2013 16:05:20 GMT]]></title><description><![CDATA[<p>Das würde mich auch interessieren :&gt;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337053</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337053</guid><dc:creator><![CDATA[SharedPTR]]></dc:creator><pubDate>Sat, 06 Jul 2013 16:05:20 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sat, 06 Jul 2013 16:27:39 GMT]]></title><description><![CDATA[<p>In diesme Fall braucht man make_shared() nicht unbedingt.<br />
Aber wenn man eine Funktion mit zwei Argumenten hat, von der eine ein shared_ptr ist, könnte es zu einem Memoryleak kommen, da die Auswertungsreihenfolge für Funktionsargumente nicht festgelegt ist. Denn wenn die Erzeugung des zweiten Argumentes eine Exception schmeißt, und von shared_ptr lediglich der new operator aufgerufen wurde, der shared_ptr selber aber noch nicht erzeugt wurde, wird der Destruktor des ptrs nicht aufgerufen und der mit new allokierte Speicher nie freigegeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337060</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337060</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 06 Jul 2013 16:27:39 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sat, 06 Jul 2013 20:03:55 GMT]]></title><description><![CDATA[<p>Man sollte <code>std::make_shared()</code> auch verwenden, weil es eine Optimierung erlaubt (nämlich Referenz-Counter und Objekt zusammen zu speichern und so nur eine dynamische Allokation zu haben).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337098</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337098</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 06 Jul 2013 20:03:55 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sun, 07 Jul 2013 07:25:46 GMT]]></title><description><![CDATA[<blockquote>
<p>Denn wenn die Erzeugung des zweiten Argumentes eine Exception schmeißt, und von shared_ptr lediglich der new operator aufgerufen wurde, der shared_ptr selber aber noch nicht erzeugt wurde</p>
</blockquote>
<p>Hmm.</p>
<p>§5.2.2/8 schrieb:</p>
<blockquote>
<p>The evaluations of the postfix expression and of the argument expressions are all unsequenced relative to one another.</p>
</blockquote>
<p>Aber wie leitest du daraus ab, dass die Auswertung eines Ausdrucks abrupt endet wenn in der Auswertung eines anderen eine Exception geworfen werden soll? Auch wenn die Ausdrücke gleichzeitig ausgewertet werden können,</p>
<p>§1.9/13 schrieb:</p>
<blockquote>
<p>The execution of unsequenced evaluations can overlap.</p>
</blockquote>
<p>werden sie komplett ausgewertet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337162</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337162</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 07 Jul 2013 07:25:46 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sun, 07 Jul 2013 07:35:53 GMT]]></title><description><![CDATA[<p><a href="http://herbsutter.com/gotw/_102/" rel="nofollow">http://herbsutter.com/gotw/_102/</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337163</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337163</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Sun, 07 Jul 2013 07:35:53 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sun, 07 Jul 2013 08:27:30 GMT]]></title><description><![CDATA[<p>krümelkacker schrieb:</p>
<blockquote>
<p><a href="http://herbsutter.com/gotw/_102/" rel="nofollow">http://herbsutter.com/gotw/_102/</a></p>
</blockquote>
<p>Bevor noch jemand denkt, dass sein C++ viel schlechter ist als er dachte:</p>
<pre><code>void f( std::unique_ptr&amp;lt;T1&amp;gt;, std::unique_ptr&amp;lt;T2&amp;gt; );
</code></pre>
<p>Das ist kein gültiges C++, auch nicht C++11. Der Mensch kann halt kein HTML (oder mein Browser), das sollte</p>
<pre><code>void f(std::unique_ptr&lt;T1&gt;, std::unique_ptr&lt;T2&gt;);
</code></pre>
<p>heißen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337173</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337173</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Sun, 07 Jul 2013 08:27:30 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sun, 07 Jul 2013 11:28:38 GMT]]></title><description><![CDATA[<p>Jetzt wo ich darüber nachdenke hat der Standard meiner Aussage nichts zu bieten. Ich dachte, es steht irgendwo, dass die Auswertung eines Ausdrucks auch definitiv die komplette Auswertung aller Sub-Ausdrücke beinhaltet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337219</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337219</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 07 Jul 2013 11:28:38 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sun, 07 Jul 2013 11:30:25 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Ich dachte, es steht irgendwo, dass die Auswertung eines Ausdrucks auch definitiv die komplette Auswertung aller Sub-Ausdrücke beinhaltet.</p>
</blockquote>
<p>Hättest nur mal an &amp;&amp; und || gedacht. :p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337221</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337221</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Sun, 07 Jul 2013 11:30:25 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sun, 07 Jul 2013 12:13:20 GMT]]></title><description><![CDATA[<p>out schrieb:</p>
<blockquote>
<p>Sone schrieb:</p>
<blockquote>
<p>Ich dachte, es steht irgendwo, dass die Auswertung eines Ausdrucks auch definitiv die komplette Auswertung aller Sub-Ausdrücke beinhaltet.</p>
</blockquote>
<p>Hättest nur mal an &amp;&amp; und || gedacht. :p</p>
</blockquote>
<p>Das meinte ich <strong>gar nicht</strong>. Ich meinte, dass wenn ein Ausdruck ausgewertet wird, definitiv alles auf einmal ausgewertet wird - sprich, wenn ich schreibe,<br />
<code>f(g(expr), h(expr2))</code> , dass dann nicht zuerst <code>expr</code> und danach <code>expr2</code> ausgewertet wird, sondern sobald die Auswertung von expr angefangen hat, auch g aufgerufen wird und definitiv nichts dazwischen. Dass sozusagen die Auswertung konsistent ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337225</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337225</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 07 Jul 2013 12:13:20 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sun, 07 Jul 2013 14:47:31 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/28547">@Sone</a><br />
Ja, das meinen viele. Ist aber in C++ nicht garantiert. Weswegen es auch so wichtig ist immer und immer wieder darauf hinzuweisen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337246</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337246</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 07 Jul 2013 14:47:31 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sun, 07 Jul 2013 16:23:05 GMT]]></title><description><![CDATA[<p>Was sind eigentlich Gründe für sowas?</p>
<p>Oder allgemeiner gefragt, wieso gibt der Standard nicht Evaluationsreihenfolgen vor?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337265</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337265</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Sun, 07 Jul 2013 16:23:05 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sun, 07 Jul 2013 16:35:11 GMT]]></title><description><![CDATA[<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>Was sind eigentlich Gründe für sowas?</p>
<p>Oder allgemeiner gefragt, wieso gibt der Standard nicht Evaluationsreihenfolgen vor?</p>
</blockquote>
<p>Dachte, weil es für den Compiler damit einfacher ist, Optimierungen vorzunehmen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337267</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337267</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Sun, 07 Jul 2013 16:35:11 GMT</pubDate></item><item><title><![CDATA[Reply to Kurze Frage zum shared_ptr Konstruktor on Sun, 07 Jul 2013 17:38:58 GMT]]></title><description><![CDATA[<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>Was sind eigentlich Gründe für sowas?</p>
<p>Oder allgemeiner gefragt, wieso gibt der Standard nicht Evaluationsreihenfolgen vor?</p>
</blockquote>
<p>Folge krümelkackers Link</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2337278</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2337278</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Sun, 07 Jul 2013 17:38:58 GMT</pubDate></item></channel></rss>