<?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[VS2010 Bug?]]></title><description><![CDATA[<p>Hallo,</p>
<p>folgender Code bereitet mir in VS2010 Probleme:</p>
<pre><code class="language-cpp">template &lt;typename Type, typename Param&gt;
class EventHandler
{
private:
	class Handler
	{
	public:
		typedef void (Handler::*Type)(Param);

		Handler *source;
		Type function;
	};

public:
	template &lt;typename T&gt;
	void Add(void *source, T function)
	{
		Handler h;
		h.source = reinterpret_cast&lt;Handler*&gt;(source);
		h.function = reinterpret_cast&lt;Handler::Type&gt;(function);
		...
	}

	...
};

class Test : std::enable_shared_from_this&lt;Test&gt;
{
public:
	EventHandler&lt;void(const std::shared_ptr&lt;Test&gt; &amp;), const
	std::shared_ptr&lt;Test&gt; &amp;&gt; e;

	Test()
	{
		e.Add(this, &amp;Test::func);
	}
	virtual ~Test() { }

	void func(const std::shared_ptr&lt;Test&gt; &amp;p) { }
};
</code></pre>
<p>Fehlermeldung:</p>
<pre><code>Error 1 error C2440: 'reinterpret_cast' : cannot convert from 'void (__thiscall Test::* )(const std::tr1::shared_ptr&lt;_Ty&gt; &amp;)' to 'void (__thiscall EventHandler&lt;Type,Param&gt;::Handler::* )(Param)'
</code></pre>
<p>Problem lässt sich lösen, wenn man entweder die Klasse Test nicht virtual macht oder std::enable_shared_from_this&lt;Test&gt; weglässt.</p>
<p>In ideone kompiliert der Code ohne Fehler (<a href="http://ideone.com/WG7f8" rel="nofollow">http://ideone.com/WG7f8</a>).</p>
<p>Bug in VS2010 oder ideone?</p>
<p>greetz KN4CK3R</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/292526/vs2010-bug</link><generator>RSS for Node</generator><lastBuildDate>Sun, 16 Aug 2026 21:02:54 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/292526.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 12 Sep 2011 06:20:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to VS2010 Bug? on Mon, 12 Sep 2011 06:20:49 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>folgender Code bereitet mir in VS2010 Probleme:</p>
<pre><code class="language-cpp">template &lt;typename Type, typename Param&gt;
class EventHandler
{
private:
	class Handler
	{
	public:
		typedef void (Handler::*Type)(Param);

		Handler *source;
		Type function;
	};

public:
	template &lt;typename T&gt;
	void Add(void *source, T function)
	{
		Handler h;
		h.source = reinterpret_cast&lt;Handler*&gt;(source);
		h.function = reinterpret_cast&lt;Handler::Type&gt;(function);
		...
	}

	...
};

class Test : std::enable_shared_from_this&lt;Test&gt;
{
public:
	EventHandler&lt;void(const std::shared_ptr&lt;Test&gt; &amp;), const
	std::shared_ptr&lt;Test&gt; &amp;&gt; e;

	Test()
	{
		e.Add(this, &amp;Test::func);
	}
	virtual ~Test() { }

	void func(const std::shared_ptr&lt;Test&gt; &amp;p) { }
};
</code></pre>
<p>Fehlermeldung:</p>
<pre><code>Error 1 error C2440: 'reinterpret_cast' : cannot convert from 'void (__thiscall Test::* )(const std::tr1::shared_ptr&lt;_Ty&gt; &amp;)' to 'void (__thiscall EventHandler&lt;Type,Param&gt;::Handler::* )(Param)'
</code></pre>
<p>Problem lässt sich lösen, wenn man entweder die Klasse Test nicht virtual macht oder std::enable_shared_from_this&lt;Test&gt; weglässt.</p>
<p>In ideone kompiliert der Code ohne Fehler (<a href="http://ideone.com/WG7f8" rel="nofollow">http://ideone.com/WG7f8</a>).</p>
<p>Bug in VS2010 oder ideone?</p>
<p>greetz KN4CK3R</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2118142</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2118142</guid><dc:creator><![CDATA[KN4CK3R]]></dc:creator><pubDate>Mon, 12 Sep 2011 06:20:49 GMT</pubDate></item><item><title><![CDATA[Reply to VS2010 Bug? on Mon, 12 Sep 2011 06:48:05 GMT]]></title><description><![CDATA[<p>Reinterpret_cast macht an der Stelle mit Sicherheit nicht das, was du erreichen willst.<br />
Warum willst du ein void* übergeben und irgendein T, damti verlierst du wertvolle Typinformationen. z.B. würde folgendes &quot;gehen&quot;:</p>
<pre><code class="language-cpp">int i;
eventHandler.Add(&amp;i, i);
</code></pre>
<p>Was willst du erreichen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2118151</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2118151</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 12 Sep 2011 06:48:05 GMT</pubDate></item><item><title><![CDATA[Reply to VS2010 Bug? on Mon, 12 Sep 2011 07:22:39 GMT]]></title><description><![CDATA[<p>mit Add lassen sich Funktionen einer Klasse dem EventHandler zuweisen. Per Invoke können diese dann von einer anderen Stelle aufgerufen werden.</p>
<pre><code class="language-cpp">void Invoke(Param param)
{
	for (auto it = handlers.begin(); it != handlers.end(); it++)
	{
		Handler &amp;handler = *it;
		(handler.source-&gt;*handler.function)(param);
	}
}
</code></pre>
<p>Also verschiedene Funktionen registrieren und dann per Invoke alle nacheinander aufrufen.</p>
<pre><code class="language-cpp">Test t;
t.e.Invoke(0); //ruft Test::func(0) auf
</code></pre>
<p>greetz KN4CK3R</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2118162</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2118162</guid><dc:creator><![CDATA[KN4CK3R]]></dc:creator><pubDate>Mon, 12 Sep 2011 07:22:39 GMT</pubDate></item><item><title><![CDATA[Reply to VS2010 Bug? on Mon, 12 Sep 2011 08:12:31 GMT]]></title><description><![CDATA[<p>MSVC 2010 kennt da schon schöne und nützliche Features des C++0x Standard: std::function und Lambdas. Damit sieht dein Code wie folgt aus:</p>
<pre><code class="language-cpp">template &lt;typename Signature&gt;
class EventHandler
{
    typedef std::function&lt;Signature&gt; Handler;
    std::vector&lt;Handler&gt; handlers;
public:
    void Add(Handler const&amp; h)
    {
         handlers.push_back(h);
    }

    template &lt;class T&gt;
    void invoke(T&amp;&amp; param)
    {
        for (auto it = handlers.begin(); it != handlers.end(); ++it)
        {
             Handler&amp; h = *it;
             h(std::forward&lt;T&gt;(param));
        }
    }
};

class Test : std::enable_shared_from_this&lt;Test&gt;
{
public:
    EventHandler&lt;void(const std::shared_ptr&lt;Test&gt; &amp;p)&gt; e;

    Test()
    {
        e.Add([this](const std::shared_ptr&lt;Test&gt; &amp;p)
        {
             func(p);
        });
        //alternativ:  
        // e.Add(std::bind(&amp;Test::func, this, std::placeholders::_1));
    }
    virtual ~Test() { }

    void func(const std::shared_ptr&lt;Test&gt; &amp;p) { }
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2118173</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2118173</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 12 Sep 2011 08:12:31 GMT</pubDate></item><item><title><![CDATA[Reply to VS2010 Bug? on Mon, 12 Sep 2011 08:22:58 GMT]]></title><description><![CDATA[<p>Irgendwie schein Laufzeitpolymorphie durch Vererbung und virtuelle Funktionen aus der Mode zu kommen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2118175</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2118175</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Mon, 12 Sep 2011 08:22:58 GMT</pubDate></item><item><title><![CDATA[Reply to VS2010 Bug? on Mon, 12 Sep 2011 08:25:40 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/9960">@pumuckl</a>: Tatsache, machts einfacher und funktioniert.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/21632">@knivil</a>: inwiefern?</p>
<p>greetz KN4CK3R</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2118177</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2118177</guid><dc:creator><![CDATA[KN4CK3R]]></dc:creator><pubDate>Mon, 12 Sep 2011 08:25:40 GMT</pubDate></item></channel></rss>