<?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[Predicate Member Function]]></title><description><![CDATA[<p>Hallo,<br />
ich möchte bei std::remove_if dem dritten Paramter mit einer Member-Function belegen.<br />
Ich habe schon herumprobiert und von Lösungen mit std::mem_fun und std::bind1st/std::bind2nd gelesen, aber bisher hat nichts funktioniert.<br />
Könnte mir jemand zeigen, wie es richtig geht?</p>
<p>Eine minimalisierte Version:</p>
<pre><code class="language-cpp">class Foo
{
public:
	void call_me();
	template &lt;int N&gt; bool mfunction(int i);
	std::vector&lt;int&gt; container;
};

template &lt;int N&gt; 
bool mfunction(int i)
{
	return N &gt; i;
}

void Foo::call_me()
{
	std::remove_if(container.begin(), container.end(), &amp;Foo::mfunction&lt;5&gt;);
}

int _tmain(int argc, _TCHAR* argv[])
{
	Foo x;
	x.call_me();
	return 0;
}
</code></pre>
<p>Compilerfehler:</p>
<blockquote>
<p>d:\programme\microsoft visual studio 10.0\vc\include\algorithm(1839): error C2064: Ausdruck ergibt keine Funktion, die 1 Argumente übernimmt<br />
1&gt; d:\programme\microsoft visual studio 10.0\vc\include\algorithm(1853): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template &quot;_FwdIt std::_Remove_if&lt;int*,_Pr&gt;(_FwdIt,_FwdIt,_Pr)&quot;.<br />
1&gt; with<br />
1&gt; [<br />
1&gt; _FwdIt=int <em>,<br />
1&gt; _Pr=bool (__thiscall Foo::</em> )(int)<br />
1&gt; ]<br />
1&gt; c:\users\wurst\documents\visual studio 2010\projects\testasdf\testasdf\testasdf.cpp(20): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template &quot;_FwdIt std::remove_if&lt;std::_Vector_iterator&lt;_Myvec&gt;,bool(__thiscall Foo::* )(int)&gt;(_FwdIt,_FwdIt,_Pr)&quot;.<br />
1&gt; with<br />
1&gt; [<br />
1&gt; _FwdIt=std::_Vector_iterator&lt;std::_Vector_val&lt;int,std::allocator&lt;int&gt;&gt;&gt;,<br />
1&gt; _Myvec=std::_Vector_val&lt;int,std::allocator&lt;int&gt;&gt;,<br />
1&gt; _Pr=bool (__thiscall Foo::* )(int)<br />
1&gt; ]<br />
1&gt;</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/topic/306239/predicate-member-function</link><generator>RSS for Node</generator><lastBuildDate>Sat, 08 Aug 2026 07:36:52 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/306239.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 22 Jul 2012 12:57:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Predicate Member Function on Sun, 22 Jul 2012 12:57:52 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich möchte bei std::remove_if dem dritten Paramter mit einer Member-Function belegen.<br />
Ich habe schon herumprobiert und von Lösungen mit std::mem_fun und std::bind1st/std::bind2nd gelesen, aber bisher hat nichts funktioniert.<br />
Könnte mir jemand zeigen, wie es richtig geht?</p>
<p>Eine minimalisierte Version:</p>
<pre><code class="language-cpp">class Foo
{
public:
	void call_me();
	template &lt;int N&gt; bool mfunction(int i);
	std::vector&lt;int&gt; container;
};

template &lt;int N&gt; 
bool mfunction(int i)
{
	return N &gt; i;
}

void Foo::call_me()
{
	std::remove_if(container.begin(), container.end(), &amp;Foo::mfunction&lt;5&gt;);
}

int _tmain(int argc, _TCHAR* argv[])
{
	Foo x;
	x.call_me();
	return 0;
}
</code></pre>
<p>Compilerfehler:</p>
<blockquote>
<p>d:\programme\microsoft visual studio 10.0\vc\include\algorithm(1839): error C2064: Ausdruck ergibt keine Funktion, die 1 Argumente übernimmt<br />
1&gt; d:\programme\microsoft visual studio 10.0\vc\include\algorithm(1853): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template &quot;_FwdIt std::_Remove_if&lt;int*,_Pr&gt;(_FwdIt,_FwdIt,_Pr)&quot;.<br />
1&gt; with<br />
1&gt; [<br />
1&gt; _FwdIt=int <em>,<br />
1&gt; _Pr=bool (__thiscall Foo::</em> )(int)<br />
1&gt; ]<br />
1&gt; c:\users\wurst\documents\visual studio 2010\projects\testasdf\testasdf\testasdf.cpp(20): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template &quot;_FwdIt std::remove_if&lt;std::_Vector_iterator&lt;_Myvec&gt;,bool(__thiscall Foo::* )(int)&gt;(_FwdIt,_FwdIt,_Pr)&quot;.<br />
1&gt; with<br />
1&gt; [<br />
1&gt; _FwdIt=std::_Vector_iterator&lt;std::_Vector_val&lt;int,std::allocator&lt;int&gt;&gt;&gt;,<br />
1&gt; _Myvec=std::_Vector_val&lt;int,std::allocator&lt;int&gt;&gt;,<br />
1&gt; _Pr=bool (__thiscall Foo::* )(int)<br />
1&gt; ]<br />
1&gt;</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2234708</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234708</guid><dc:creator><![CDATA[Wurstinator]]></dc:creator><pubDate>Sun, 22 Jul 2012 12:57:52 GMT</pubDate></item><item><title><![CDATA[Reply to Predicate Member Function on Sun, 22 Jul 2012 13:10:48 GMT]]></title><description><![CDATA[<p><strong>Lösung für C++98/03</strong></p>
<pre><code class="language-cpp">#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;functional&gt;

class Foo
{
public:
    void call_me();
    template &lt;int N&gt; bool mfunction(int i);
    std::vector&lt;int&gt; container;
};

void Foo::call_me()
{
    std::remove_if(container.begin(), container.end(), std::bind1st(std::mem_fun(&amp;Foo::mfunction&lt;5&gt;), this));
}

template &lt;int N&gt;
bool Foo::mfunction(int i)
{
    return N &gt; i;
}

int main()
{
    Foo x;
    x.call_me();
    return 0;
}
</code></pre>
<p><strong>Lösung für C++11</strong></p>
<pre><code class="language-cpp">#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;functional&gt;

class Foo
{
public:
    void call_me();
    template &lt;int N&gt; bool mfunction(int i);
    std::vector&lt;int&gt; container;
};

void Foo::call_me()
{
    std::remove_if(container.begin(), container.end(), std::bind(&amp;Foo::mfunction&lt;5&gt;, this, std::placeholders::_1));
}

template &lt;int N&gt;
bool Foo::mfunction(int i)
{
    return N &gt; i;
}

int main()
{
    Foo x;
    x.call_me();
    return 0;
}
</code></pre>
<p>Beide Codes kompilieren mit GCC 4.7.1 und clang 3.2</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234710</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234710</guid><dc:creator><![CDATA[pyhax]]></dc:creator><pubDate>Sun, 22 Jul 2012 13:10:48 GMT</pubDate></item><item><title><![CDATA[Reply to Predicate Member Function on Sun, 22 Jul 2012 13:17:52 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">std::remove_if(container.begin(), container.end(), [this] (int i) { return mfunction&lt;5&gt;(i); });
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2234714</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234714</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sun, 22 Jul 2012 13:17:52 GMT</pubDate></item><item><title><![CDATA[Reply to Predicate Member Function on Sun, 22 Jul 2012 13:32:38 GMT]]></title><description><![CDATA[<p>C++11 hab ich leider nicht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Die Lösung von pyhax hatte ich bereits probiert, was im richtigen Code einen Fehler verursacht. Weitere Google-Nachforschungen haben mir die Ursache genannt:<br />
bind1st mag keine Referenzen als Parameter.</p>
<p>Das Minimalbeispiel hätte also eher so aussehen müssen:</p>
<pre><code class="language-cpp">class Foo
{
public:
	void call_me();
	template &lt;int N&gt; bool mfunction(const int&amp; i);
	std::vector&lt;int&gt; container;
};

template &lt;int N&gt; 
bool Foo::mfunction(const int&amp; i)
{
	return N &gt; i;
}

void Foo::call_me()
{
	std::remove_if(container.begin(), container.end(), std::bind1st(std::mem_fun(&amp;Foo::mfunction&lt;5&gt;), this));
}

int _tmain(int argc, _TCHAR* argv[])
{
	Foo x;
	x.call_me();
	return 0;
}
</code></pre>
<p>Gibt es dafür auch eine Lösung oder muss ich call by value in Kauf nehmen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234719</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234719</guid><dc:creator><![CDATA[Wurstinator]]></dc:creator><pubDate>Sun, 22 Jul 2012 13:32:38 GMT</pubDate></item><item><title><![CDATA[Reply to Predicate Member Function on Sun, 22 Jul 2012 15:27:52 GMT]]></title><description><![CDATA[<p>Nimm<a href="http://www.boost.org/libs/function" rel="nofollow"> <code>boost::function</code> </a>als Zwischenlösung, das funktioniert gleich wie <code>std::function</code> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234742</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234742</guid><dc:creator><![CDATA[bstr]]></dc:creator><pubDate>Sun, 22 Jul 2012 15:27:52 GMT</pubDate></item><item><title><![CDATA[Reply to Predicate Member Function on Sun, 22 Jul 2012 15:55:15 GMT]]></title><description><![CDATA[<p>Nimm boost.bind und dann die c++11 Lösung (mit boost::bind statt std::bind + andere Placeholder namen)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234745</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234745</guid><dc:creator><![CDATA[pyhax]]></dc:creator><pubDate>Sun, 22 Jul 2012 15:55:15 GMT</pubDate></item><item><title><![CDATA[Reply to Predicate Member Function on Sun, 22 Jul 2012 17:05:55 GMT]]></title><description><![CDATA[<p>VS2010 müsste doch ein std::tr1::bind haben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234759</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234759</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Sun, 22 Jul 2012 17:05:55 GMT</pubDate></item><item><title><![CDATA[Reply to Predicate Member Function on Sun, 22 Jul 2012 18:20:09 GMT]]></title><description><![CDATA[<p>Stimmt, bind() ist offensichtlich im TR1 auch in VS2010 enthalten. Damit kann ich doch die &quot;C++11&quot;-Lösung nehmen.</p>
<p>Danke für die Antworten <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234773</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234773</guid><dc:creator><![CDATA[Wurstinator]]></dc:creator><pubDate>Sun, 22 Jul 2012 18:20:09 GMT</pubDate></item><item><title><![CDATA[Reply to Predicate Member Function on Sun, 22 Jul 2012 19:18:48 GMT]]></title><description><![CDATA[<p>VS2010 hat doch schon std::bind...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234786</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234786</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Sun, 22 Jul 2012 19:18:48 GMT</pubDate></item></channel></rss>