<?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[boost::bind + boost::shared_ptr]]></title><description><![CDATA[<p>Ich versuche der Funktion zum Freigeben einer Ressource einen Parameter zu binden. Ist das überhaupt möglich?</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;boost/bind.hpp&gt;
#include &lt;boost/shared_ptr.hpp&gt;

void Release(int*, const std::string&amp; txt)
{
	std::cout &lt;&lt; txt &lt;&lt; std::endl;
}

int main()
{
	boost::shared_ptr&lt;int&gt; p((int*)0, boost::bind(Release, _2)(&quot;Hallo Welt&quot;));
	// Funktioniert so leider nicht
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/181995/boost-bind-boost-shared_ptr</link><generator>RSS for Node</generator><lastBuildDate>Tue, 22 Sep 2026 10:51:21 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/181995.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 20 May 2007 00:45:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to boost::bind + boost::shared_ptr on Sun, 20 May 2007 00:46:37 GMT]]></title><description><![CDATA[<p>Ich versuche der Funktion zum Freigeben einer Ressource einen Parameter zu binden. Ist das überhaupt möglich?</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;boost/bind.hpp&gt;
#include &lt;boost/shared_ptr.hpp&gt;

void Release(int*, const std::string&amp; txt)
{
	std::cout &lt;&lt; txt &lt;&lt; std::endl;
}

int main()
{
	boost::shared_ptr&lt;int&gt; p((int*)0, boost::bind(Release, _2)(&quot;Hallo Welt&quot;));
	// Funktioniert so leider nicht
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1288261</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1288261</guid><dc:creator><![CDATA[joomoo]]></dc:creator><pubDate>Sun, 20 May 2007 00:46:37 GMT</pubDate></item><item><title><![CDATA[Reply to boost::bind + boost::shared_ptr on Sun, 20 May 2007 00:50:05 GMT]]></title><description><![CDATA[<p>Was willst du machen? Welchen Sinn soll das haben? Verwechselst du da nicht etwas mit boost::function???</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1288264</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1288264</guid><dc:creator><![CDATA[Artchi]]></dc:creator><pubDate>Sun, 20 May 2007 00:50:05 GMT</pubDate></item><item><title><![CDATA[Reply to boost::bind + boost::shared_ptr on Sun, 20 May 2007 00:53:45 GMT]]></title><description><![CDATA[<p>Ich will eine Funktion mit mehreren Parametern an boost::shared_ptr übergeben.</p>
<p>Das ganze hat sich erledigt, ich hab irgendwie boost::bind nicht ganz richtig verstanden, so klappt's:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;boost/bind.hpp&gt;
#include &lt;boost/shared_ptr.hpp&gt;

void Release(int*, const std::string&amp; txt)
{
	std::cout &lt;&lt; txt &lt;&lt; std::endl;
}

int main()
{
	boost::shared_ptr&lt;int&gt; p((int*)0, boost::bind(Release, _1, &quot;Hallo Welt&quot;));
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1288267</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1288267</guid><dc:creator><![CDATA[joomoo]]></dc:creator><pubDate>Sun, 20 May 2007 00:53:45 GMT</pubDate></item><item><title><![CDATA[Reply to boost::bind + boost::shared_ptr on Sun, 20 May 2007 02:08:58 GMT]]></title><description><![CDATA[<p>Genau, so klappts. Allerdings kommt man meist auch schön hin indem man sich einfach einen kleinen RAII Wrapper schreibt für die Resourcen die man verwalten möchte.<br />
Wenn ich z.B. Win32 handles verwalten möchte nehme ich nicht einfach shared_ptr mit CloseHandle als deleter, sondern einen kleinen Wrapper ala so:</p>
<pre><code class="language-cpp">struct Win32Handle
{
    explicit Win32Handle(HANDLE h = 0) : m_h(h) {}

    ~Win32Handle()
    {
        if (m_h != 0 &amp;&amp; m_h != INVALID_HANDLE_VALUE)
        {
            BOOL rc = ::CloseHandle(m_h);
            assert(rc);
        }
    }

    HANDLE m_h; // um es RAII nennen zu dürfen müsste m_h wohl const sein,
                // ist aber oft praktisch wenn es nicht const ist,
                // in dem Fall muss man halt u.U. selbst CloseHandle aufrufen
                // wenn man m_h modifiziert (das alte handle freigeben z.B.)
};
</code></pre>
<p>Das kann man dann schön mit shared_ptr verwenden, oder auch einfach als lokale Variable verwenden (ohne den Overhead von shared_ptr, was immerhin mindestens 2x new + 2x delete ist - also nicht ganz ohne).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1288272</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1288272</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 20 May 2007 02:08:58 GMT</pubDate></item><item><title><![CDATA[Reply to boost::bind + boost::shared_ptr on Sun, 20 May 2007 08:50:27 GMT]]></title><description><![CDATA[<p>Das wär wahrscheinlich besser, allerdings wird mein Handle erst spät initialisiert, deswegen mach ich es so:</p>
<pre><code class="language-cpp">static void ReleaseDC(HWND hwnd, HDC hdc)
{
	if(!::ReleaseDC(hwnd, hdc))
		std::cerr &lt;&lt; &quot;Release Device Context Failed.&quot; &lt;&lt; std::endl;
}
// ...
boost::shared_ptr&lt;boost::remove_pointer&lt;HDC&gt;::type&gt; pDeviceContext_;
// ...
pDeviceContext_.reset(GetDC(hwnd), boost::bind(ReleaseDC, hwnd, _1));
</code></pre>
<p>Gibt's eigentlich ne schnellere Version als shared_ptr, die auch eine eigene Release-Funktion unterstützt? (Tut scoped_ptr ja glaub ich net)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1288294</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1288294</guid><dc:creator><![CDATA[joomoo]]></dc:creator><pubDate>Sun, 20 May 2007 08:50:27 GMT</pubDate></item><item><title><![CDATA[Reply to boost::bind + boost::shared_ptr on Sun, 20 May 2007 16:10:11 GMT]]></title><description><![CDATA[<p>Ich sehe nicht warum das mit einer kleinen Wrapper Klasse nicht gehen sollte, aber egal.</p>
<p>Und nein, ich kenne nix was einen deleter unterstützt und schneller als shared_ptr ist, aber wie gesagt, man kann sich ja in ein paar Zeilen selbst was schreiben was die Resource kapselt - dann funktioniert es auch mit scoped_ptr.</p>
<p>BTW: bei Handles (seis nun GDI oder KERNEL32) fallen die 2 new + delete die bei shared_ptr anfallen nicht so ins Gewicht, die Funktionen sind sowieso nicht gerade schnell. Genauso Dinge wie GetDC/ReleaseDC. Mach dir also nicht allzuviel Sorgen deswegen - wenn shared_ptr deiner Meinung nach Sinn macht, verwende es einfach.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1288562</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1288562</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 20 May 2007 16:10:11 GMT</pubDate></item></channel></rss>