<?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[std::function und Funktor mit Gedächtnis]]></title><description><![CDATA[<p>Hallo,</p>
<p>wenn ich das folgende ausführe:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;functional&gt;

struct functor
{
	int value = 0;
	int operator()(int i) { return value += i; }
};

class test
{
	std::function&lt;int(int)&gt; f_;
public:
	test(std::function&lt;int(int)&gt; f) : f_(f) {}
	void foo() { f_(3); }
};

int main()
{
	functor func;
	test t(func);
	t.foo();

	std::cout &lt;&lt; func.value &lt;&lt; '\n'; // Outputs 0
}
</code></pre>
<p>erhalte ich als Ausgabe &quot;0&quot;. Ich würde aber gerne &quot;3&quot; als Ausgabe erhalten (also das &quot;Gedächtnis&quot; des Funktors erhalten). Geht das mit std::function als Argument überhaupt oder hab ich da einen Denkfehler drin?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/332826/std-function-und-funktor-mit-gedächtnis</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Apr 2026 10:55:34 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/332826.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 24 May 2015 14:09:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::function und Funktor mit Gedächtnis on Sun, 24 May 2015 14:09:49 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>wenn ich das folgende ausführe:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;functional&gt;

struct functor
{
	int value = 0;
	int operator()(int i) { return value += i; }
};

class test
{
	std::function&lt;int(int)&gt; f_;
public:
	test(std::function&lt;int(int)&gt; f) : f_(f) {}
	void foo() { f_(3); }
};

int main()
{
	functor func;
	test t(func);
	t.foo();

	std::cout &lt;&lt; func.value &lt;&lt; '\n'; // Outputs 0
}
</code></pre>
<p>erhalte ich als Ausgabe &quot;0&quot;. Ich würde aber gerne &quot;3&quot; als Ausgabe erhalten (also das &quot;Gedächtnis&quot; des Funktors erhalten). Geht das mit std::function als Argument überhaupt oder hab ich da einen Denkfehler drin?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454613</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454613</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sun, 24 May 2015 14:09:49 GMT</pubDate></item><item><title><![CDATA[Reply to std::function und Funktor mit Gedächtnis on Sun, 24 May 2015 14:39:41 GMT]]></title><description><![CDATA[<p>Dein Denkfehler ist, dass dein struct 'functor' kopiert wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454619</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454619</guid><dc:creator><![CDATA[tkausl]]></dc:creator><pubDate>Sun, 24 May 2015 14:39:41 GMT</pubDate></item><item><title><![CDATA[Reply to std::function und Funktor mit Gedächtnis on Sun, 24 May 2015 14:44:30 GMT]]></title><description><![CDATA[<p>tkausl schrieb:</p>
<blockquote>
<p>Dein Denkfehler ist, dass dein struct 'functor' kopiert wird.</p>
</blockquote>
<p>Ok, und wie sage ich ihm dann dass eine Referenz von dem functor func verwendet werden soll und keine Kopie?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454620</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454620</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sun, 24 May 2015 14:44:30 GMT</pubDate></item><item><title><![CDATA[Reply to std::function und Funktor mit Gedächtnis on Sun, 24 May 2015 15:16:28 GMT]]></title><description><![CDATA[<pre><code>test t(std::ref(func));
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2454625</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454625</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sun, 24 May 2015 15:16:28 GMT</pubDate></item><item><title><![CDATA[Reply to std::function und Funktor mit Gedächtnis on Sun, 24 May 2015 15:49:51 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<pre><code>test t(std::ref(func));
</code></pre>
</blockquote>
<p>Lol.</p>
<p>Vielen Dank, das klappt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454631</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454631</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sun, 24 May 2015 15:49:51 GMT</pubDate></item><item><title><![CDATA[Reply to std::function und Funktor mit Gedächtnis on Sun, 24 May 2015 16:21:04 GMT]]></title><description><![CDATA[<p>Um die Lösung zu verstehen musst du dir einmal die Deklaration des Konstruktors von std::function anschauen:</p>
<pre><code>template&lt; class F &gt;
function( F f );
</code></pre>
<p>Dort werden über einen Template Parameter beliebige Funktionen/Funktionsobjekte akzeptiert. Bei einem so geschriebenen Konstruktor wird für F aber niemals ein Typ mit Referenz abgeleitet. Daher gabs es bei deiner ursprünglichen Lösung eine Kopie deines Objekts. Man muss also irgendwie dafür sorgen, dass der Konstruktor dein Objekt als Referenz übernimmt und speichert. Da es meistens böse ist Typen für Template Funktionen explizit anzugeben, und für Konstruktoren gar nicht möglich ist, brauchen wir also eine andere Möglichkeit dafür zu sorgen, dass F wenigstens sowas ähnliches wie eine Referenz wird. Dafür gibt es die Klasse std::reference_wrapper. Durch die Funktion std::ref kann man sich komfortabel ein Objekt dieser Klasse erzeugen. Der Typ F der also letztendlich an den Konstruktor von std::function übergeben wird lautet: std::reference_wrapper&lt;functor&gt;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454636</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454636</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Sun, 24 May 2015 16:21:04 GMT</pubDate></item><item><title><![CDATA[Reply to std::function und Funktor mit Gedächtnis on Sun, 24 May 2015 16:30:53 GMT]]></title><description><![CDATA[<p>Ok, das macht Sinn, danke <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
<p>Wollte schon alles auf static umstellen, aber damit geht es jetzt <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2454639</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454639</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sun, 24 May 2015 16:30:53 GMT</pubDate></item><item><title><![CDATA[Reply to std::function und Funktor mit Gedächtnis on Sun, 24 May 2015 16:32:52 GMT]]></title><description><![CDATA[<p>happystudent schrieb:</p>
<blockquote>
<p>Wollte schon alles auf static umstellen</p>
</blockquote>
<p>OMG <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454640</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454640</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 24 May 2015 16:32:52 GMT</pubDate></item><item><title><![CDATA[Reply to std::function und Funktor mit Gedächtnis on Sun, 24 May 2015 16:57:44 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>OMG <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
</blockquote>
<p>Hehehe <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>
<p>Ja, nur temporär bis ich ne Lösung gefunden habe (dachte nicht dass es so einfach geht).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454642</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454642</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sun, 24 May 2015 16:57:44 GMT</pubDate></item></channel></rss>