<?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[bind2nd problem]]></title><description><![CDATA[<p>hi leute,<br />
ich würde zur übung gerne einen eigenen kleinen quicksort schreiben:</p>
<pre><code>struct some_type;
typedef some_type value_type;
typedef std::vector &lt;value_type&gt; container_type;
typedef container_type::iterator iterator_type;
typedef bool (* compare_type) (value_type const &amp;, value_type const &amp;);
bool compare (value_type const &amp; lhs, value_type const &amp; rhs);
void sort (iterator_type begin, iterator_type end, compare_type compare);

void sort (iterator_type begin, iterator_type end, compare_type compare) {
    if (begin != end) {
		iterator_type pivot = std::partition(begin, end, std::bind2nd(compare, *begin));
        sort (begin, pivot, compare);
		sort (std::max(begin + 1, pivot), end, compare);
    }
}
</code></pre>
<p>die hilfsfunktion compare:</p>
<pre><code>bool compare (value_type const &amp; lhs, value_type const &amp; rhs) {
	return lhs.key &lt; rhs.key;
}
</code></pre>
<p>anscheinend kann ich &quot;compare&quot; als binäre funktion nicht direkt an &quot;partition&quot; übergeben, da eine unäre funktion erwartet wird. daher wird bind2nd verwendet.</p>
<p>der compiler spuckt nun folgende fehlermeldung aus:</p>
<blockquote>
<p>error C2825: '_Fn2': must be a class or namespace when followed by '::'</p>
</blockquote>
<p>was mache ich falsch? warum nimmt bind2nd mein compare nicht an?<br />
die schnittstelle von compare darf übrigens nicht verändert werden... wie bringe ich den code zum laufen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/129255/bind2nd-problem</link><generator>RSS for Node</generator><lastBuildDate>Wed, 26 Aug 2026 06:06:19 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/129255.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 11 Dec 2005 21:58:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to bind2nd problem on Sun, 11 Dec 2005 22:00:38 GMT]]></title><description><![CDATA[<p>hi leute,<br />
ich würde zur übung gerne einen eigenen kleinen quicksort schreiben:</p>
<pre><code>struct some_type;
typedef some_type value_type;
typedef std::vector &lt;value_type&gt; container_type;
typedef container_type::iterator iterator_type;
typedef bool (* compare_type) (value_type const &amp;, value_type const &amp;);
bool compare (value_type const &amp; lhs, value_type const &amp; rhs);
void sort (iterator_type begin, iterator_type end, compare_type compare);

void sort (iterator_type begin, iterator_type end, compare_type compare) {
    if (begin != end) {
		iterator_type pivot = std::partition(begin, end, std::bind2nd(compare, *begin));
        sort (begin, pivot, compare);
		sort (std::max(begin + 1, pivot), end, compare);
    }
}
</code></pre>
<p>die hilfsfunktion compare:</p>
<pre><code>bool compare (value_type const &amp; lhs, value_type const &amp; rhs) {
	return lhs.key &lt; rhs.key;
}
</code></pre>
<p>anscheinend kann ich &quot;compare&quot; als binäre funktion nicht direkt an &quot;partition&quot; übergeben, da eine unäre funktion erwartet wird. daher wird bind2nd verwendet.</p>
<p>der compiler spuckt nun folgende fehlermeldung aus:</p>
<blockquote>
<p>error C2825: '_Fn2': must be a class or namespace when followed by '::'</p>
</blockquote>
<p>was mache ich falsch? warum nimmt bind2nd mein compare nicht an?<br />
die schnittstelle von compare darf übrigens nicht verändert werden... wie bringe ich den code zum laufen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/939875</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939875</guid><dc:creator><![CDATA[thomassteiner]]></dc:creator><pubDate>Sun, 11 Dec 2005 22:00:38 GMT</pubDate></item><item><title><![CDATA[Reply to bind2nd problem on Sun, 11 Dec 2005 22:23:50 GMT]]></title><description><![CDATA[<p>Anstelle des Typedefs solltest du ein Template verwenden, das macht die Sache schonmal viel flexibler. Hab nicht weiter hingeschaun, hoffe das hilft dir mal weiter.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/939886</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939886</guid><dc:creator><![CDATA[FireFlow]]></dc:creator><pubDate>Sun, 11 Dec 2005 22:23:50 GMT</pubDate></item><item><title><![CDATA[Reply to bind2nd problem on Sun, 11 Dec 2005 23:42:47 GMT]]></title><description><![CDATA[<p>thomassteiner schrieb:</p>
<blockquote>
<pre><code>iterator_type pivot = std::partition(begin, end, std::bind2nd(compare, *begin));
</code></pre>
<p>anscheinend kann ich &quot;compare&quot; als binäre funktion nicht direkt an &quot;partition&quot; übergeben, da eine unäre funktion erwartet wird. daher wird bind2nd verwendet.</p>
<p>der compiler spuckt nun folgende fehlermeldung aus:</p>
<blockquote>
<p>error C2825: '_Fn2': must be a class or namespace when followed by '::'</p>
</blockquote>
<p>was mache ich falsch? warum nimmt bind2nd mein compare nicht an?</p>
</blockquote>
<p>Hi Thomas,</p>
<p>std::bind2nd erwartet lt. Standard bestimmte typedefs. Dazu werden Adapter zur Verfügung gestellt, die diese typedefs generieren. Dein Helferlein heißt hier: std::ptr_fun, der einen pointer_to_binary_function generiert.</p>
<pre><code class="language-cpp">iterator_type pivot = std::partition(begin, end, std::bind2nd( std::ptr_fun( compare ), *begin));
</code></pre>
<p>Dummerweise landest Du dann beim 'reference to reference'-Problem; siehe hier <a href="http://www.boost.org/libs/utility/call_traits.htm" rel="nofollow">http://www.boost.org/libs/utility/call_traits.htm</a>.</p>
<p>thomassteiner schrieb:</p>
<blockquote>
<p>die schnittstelle von compare darf übrigens nicht verändert werden... wie bringe ich den code zum laufen?</p>
</blockquote>
<p>am besten Du nimmst gleich <a href="http://www.boost.org/libs/bind/bind.html" rel="nofollow">boost::bind</a> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f576.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--sunglasses"
      title=":sunglasses:"
      alt="🕶"
    /></p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/939910</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939910</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Sun, 11 Dec 2005 23:42:47 GMT</pubDate></item><item><title><![CDATA[Reply to bind2nd problem on Sun, 11 Dec 2005 23:50:30 GMT]]></title><description><![CDATA[<p>Am besten nimmst du gleich einen Funktor.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/939912</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939912</guid><dc:creator><![CDATA[tipp]]></dc:creator><pubDate>Sun, 11 Dec 2005 23:50:30 GMT</pubDate></item><item><title><![CDATA[Reply to bind2nd problem on Tue, 13 Dec 2005 18:52:47 GMT]]></title><description><![CDATA[<p>Genau. Und den musst du von binary_function ableiten, damit er adaptable wird (das ist das, was bind2nd verlangt).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/941342</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/941342</guid><dc:creator><![CDATA[Ringding]]></dc:creator><pubDate>Tue, 13 Dec 2005 18:52:47 GMT</pubDate></item><item><title><![CDATA[Reply to bind2nd problem on Tue, 13 Dec 2005 22:08:38 GMT]]></title><description><![CDATA[<p>Ringding schrieb:</p>
<blockquote>
<p>Genau. Und den musst du von binary_function ableiten, damit er adaptable wird (das ist das, was bind2nd verlangt).</p>
</blockquote>
<p>Im Prinzip ja, aber doch wieder nein :xmas1: , weil so landet man ohne besondere Verenkungen wieder beim 'reference of reference'-Problem. Wenn schon Funktor, dann ein Funktor der das bind2nd mit einschließt. Etwa so:</p>
<pre><code class="language-cpp">template&lt; typename T &gt;
struct Comp
{
    typedef bool (*comp_type)( const T&amp; a, const T&amp; b );
    Comp( comp_type comp, const T&amp; fix ) : m_comp( comp ), m_fix( fix ) {}
    bool operator()( const T&amp; x ) const
    {
        return (*m_comp)( x, m_fix );
    }
private:
    comp_type m_comp;
    T   m_fix;
};
// Factory-Funktion
template&lt; typename T &gt;
Comp&lt; T &gt; make_comp( typename Comp&lt; T &gt;::comp_type comp_func, const T&amp; fix )
{
    return Comp&lt; T &gt;( comp_func, fix );
}
</code></pre>
<p>und so in der Anwendung</p>
<pre><code class="language-cpp">//        iterator_type pivot = std::partition(begin, end, std::bind2nd(compare, *begin));
        iterator_type pivot = std::partition(begin, end, make_comp( compare, *begin ) );
</code></pre>
<p>.. mit <a href="http://www.boost.org/libs/bind/bind.html" rel="nofollow">boost</a> wär' alles einfacher, hier schreibt man einfach</p>
<pre><code class="language-cpp">iterator_type pivot = std::partition(begin, end, boost::bind( compare, _1, *begin ) );
</code></pre>
<p>:xmas2: Werner</p>
<p>PS.: übrigens; die Weihnachts-Icons sind <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f576.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--sunglasses"
      title=":sunglasses:"
      alt="🕶"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/941518</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/941518</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Tue, 13 Dec 2005 22:08:38 GMT</pubDate></item><item><title><![CDATA[Reply to bind2nd problem on Tue, 13 Dec 2005 22:47:24 GMT]]></title><description><![CDATA[<p>Da boost::bind seit längerem definitiv im TR1 des ISO C++ drin ist, kann man es bedenkenlos benutzen. Wird also eh bald bei jedem Compiler (auch VC++) mit beiligen. Nur halt unter dem Namespace std::tr1 und nicht boost::.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/941533</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/941533</guid><dc:creator><![CDATA[Artchi]]></dc:creator><pubDate>Tue, 13 Dec 2005 22:47:24 GMT</pubDate></item></channel></rss>