<?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[Curiouses bei der Verwendung von Templates]]></title><description><![CDATA[<p>Hi Forum,</p>
<p>ich habe ein Problem mit der Verwendung von Templates im folgenden Code und zwar nicht im Delegate sondern im Speziellen in der Klasse B.</p>
<pre><code class="language-cpp">template&lt;typename parameter&gt;
class Delegate {
    typedef void (*invoke_stub)(void const *, parameter);
    void const  *obj_ptr_;
    invoke_stub stub_ptr_;

    template&lt;typename T, void (T::*Fxn)(parameter)&gt;
    struct mem_fn_stub
    {
        static void invoke(void const * obj_ptr, parameter a0)
        {
            T * obj = static_cast&lt;T *&gt;( const_cast&lt;void *&gt;( obj_ptr ) );
            (obj-&gt;*Fxn)( a0 );
        }
    };

    template&lt;void (*Fxn)(parameter)&gt;
    struct function_stub
    {
        static void invoke(void const *, parameter a0)
        {
            (*Fxn)( a0 );
        }
    };

public:
    Delegate() : obj_ptr_( 0 ), stub_ptr_( 0 ) { }

    template&lt;typename T, void (T::*Fxn)(parameter)&gt;
    void bind(T * obj)
    {
        obj_ptr_ = const_cast&lt;T const *&gt;( obj );
        stub_ptr_ = &amp;mem_fn_stub&lt;T, Fxn&gt;::invoke;
    }

    template&lt;void (*Fxn)(parameter)&gt;
    void bind()
    {
        obj_ptr_ = 0;
        stub_ptr_ = &amp;function_stub&lt;Fxn&gt;::invoke;
    }

    void operator ()(parameter a0) const
    {
        ( *stub_ptr_ )( obj_ptr_, a0 );
    }
};

void cb(int&amp; cbd) {
}

class A {
public:
    Delegate&lt;int&amp;&gt; callback;
};

template &lt;class T&gt;
class B {
    typedef T TypeT;
//    typedef A TypeT;
public:
    void cb(int&amp;) {}

    B() {
        int i=5;
        TypeT *_t=new TypeT();
        _t-&gt;callback.bind&lt;&amp;::cb&gt;();
        _t-&gt;callback(i);
        _t-&gt;callback.bind&lt;B,&amp;B::cb&gt;(this);
        _t-&gt;callback(i);

    }
};

int main (int argc, char **argv) {
    B&lt;A&gt; b;
    return 0;
}
</code></pre>
<p>Wenn ich das typedef aus Zeile 61 benutze, wo das Typedef direkt die Klasse A verwendet, wird der Code ordnungsgemaess uebersetzt und funktioniert auch einwandfrei. Wenn ich hingegen das typedef mit dem Template-Parameter verwende erhalte ich folgende Compiler-Fehler.</p>
<p><a href="http://curious.cc" rel="nofollow">curious.cc</a>: In constructor ‘B&lt;T&gt;::B()’:<br />
<a href="http://curious.cc:67" rel="nofollow">curious.cc:67</a>: error: expected primary-expression before ‘)’ token<br />
<a href="http://curious.cc:69" rel="nofollow">curious.cc:69</a>: error: expected primary-expression before ‘,’ token<br />
<a href="http://curious.cc" rel="nofollow">curious.cc</a>: In constructor ‘B&lt;T&gt;::B() [with T = A]’:<br />
<a href="http://curious.cc:76" rel="nofollow">curious.cc:76</a>: instantiated from here<br />
<a href="http://curious.cc:67" rel="nofollow">curious.cc:67</a>: error: invalid operands of types ‘&lt;unresolved overloaded function type&gt;’ and ‘void (<em>)(int&amp;)’ to binary ‘operator&lt;’<br />
<a href="http://curious.cc:76" rel="nofollow">curious.cc:76</a>: instantiated from here<br />
<a href="http://curious.cc:69" rel="nofollow">curious.cc:69</a>: error: invalid operands of types ‘void (B&lt;A&gt;::</em>)(int&amp;)’ and ‘B&lt;A&gt;* const’ to binary ‘operator&gt;’</p>
<p>Ich weiss nicht, was an dem Code falsch ist und hoffe, Ihr koennt mir helfen, denn ich weiss momentan echt nicht, wo und an welcher ecke ich das Problem angehen kann. Mir sagt in diesem Zusammenhang die Compilermeldung leider auch nicht so richtig was.</p>
<p>Danke schon mal im Voraus<br />
Michael</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/229722/curiouses-bei-der-verwendung-von-templates</link><generator>RSS for Node</generator><lastBuildDate>Sun, 27 Sep 2026 18:13:51 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/229722.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 15 Dec 2008 19:08:29 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Curiouses bei der Verwendung von Templates on Mon, 15 Dec 2008 19:08:29 GMT]]></title><description><![CDATA[<p>Hi Forum,</p>
<p>ich habe ein Problem mit der Verwendung von Templates im folgenden Code und zwar nicht im Delegate sondern im Speziellen in der Klasse B.</p>
<pre><code class="language-cpp">template&lt;typename parameter&gt;
class Delegate {
    typedef void (*invoke_stub)(void const *, parameter);
    void const  *obj_ptr_;
    invoke_stub stub_ptr_;

    template&lt;typename T, void (T::*Fxn)(parameter)&gt;
    struct mem_fn_stub
    {
        static void invoke(void const * obj_ptr, parameter a0)
        {
            T * obj = static_cast&lt;T *&gt;( const_cast&lt;void *&gt;( obj_ptr ) );
            (obj-&gt;*Fxn)( a0 );
        }
    };

    template&lt;void (*Fxn)(parameter)&gt;
    struct function_stub
    {
        static void invoke(void const *, parameter a0)
        {
            (*Fxn)( a0 );
        }
    };

public:
    Delegate() : obj_ptr_( 0 ), stub_ptr_( 0 ) { }

    template&lt;typename T, void (T::*Fxn)(parameter)&gt;
    void bind(T * obj)
    {
        obj_ptr_ = const_cast&lt;T const *&gt;( obj );
        stub_ptr_ = &amp;mem_fn_stub&lt;T, Fxn&gt;::invoke;
    }

    template&lt;void (*Fxn)(parameter)&gt;
    void bind()
    {
        obj_ptr_ = 0;
        stub_ptr_ = &amp;function_stub&lt;Fxn&gt;::invoke;
    }

    void operator ()(parameter a0) const
    {
        ( *stub_ptr_ )( obj_ptr_, a0 );
    }
};

void cb(int&amp; cbd) {
}

class A {
public:
    Delegate&lt;int&amp;&gt; callback;
};

template &lt;class T&gt;
class B {
    typedef T TypeT;
//    typedef A TypeT;
public:
    void cb(int&amp;) {}

    B() {
        int i=5;
        TypeT *_t=new TypeT();
        _t-&gt;callback.bind&lt;&amp;::cb&gt;();
        _t-&gt;callback(i);
        _t-&gt;callback.bind&lt;B,&amp;B::cb&gt;(this);
        _t-&gt;callback(i);

    }
};

int main (int argc, char **argv) {
    B&lt;A&gt; b;
    return 0;
}
</code></pre>
<p>Wenn ich das typedef aus Zeile 61 benutze, wo das Typedef direkt die Klasse A verwendet, wird der Code ordnungsgemaess uebersetzt und funktioniert auch einwandfrei. Wenn ich hingegen das typedef mit dem Template-Parameter verwende erhalte ich folgende Compiler-Fehler.</p>
<p><a href="http://curious.cc" rel="nofollow">curious.cc</a>: In constructor ‘B&lt;T&gt;::B()’:<br />
<a href="http://curious.cc:67" rel="nofollow">curious.cc:67</a>: error: expected primary-expression before ‘)’ token<br />
<a href="http://curious.cc:69" rel="nofollow">curious.cc:69</a>: error: expected primary-expression before ‘,’ token<br />
<a href="http://curious.cc" rel="nofollow">curious.cc</a>: In constructor ‘B&lt;T&gt;::B() [with T = A]’:<br />
<a href="http://curious.cc:76" rel="nofollow">curious.cc:76</a>: instantiated from here<br />
<a href="http://curious.cc:67" rel="nofollow">curious.cc:67</a>: error: invalid operands of types ‘&lt;unresolved overloaded function type&gt;’ and ‘void (<em>)(int&amp;)’ to binary ‘operator&lt;’<br />
<a href="http://curious.cc:76" rel="nofollow">curious.cc:76</a>: instantiated from here<br />
<a href="http://curious.cc:69" rel="nofollow">curious.cc:69</a>: error: invalid operands of types ‘void (B&lt;A&gt;::</em>)(int&amp;)’ and ‘B&lt;A&gt;* const’ to binary ‘operator&gt;’</p>
<p>Ich weiss nicht, was an dem Code falsch ist und hoffe, Ihr koennt mir helfen, denn ich weiss momentan echt nicht, wo und an welcher ecke ich das Problem angehen kann. Mir sagt in diesem Zusammenhang die Compilermeldung leider auch nicht so richtig was.</p>
<p>Danke schon mal im Voraus<br />
Michael</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1630936</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1630936</guid><dc:creator><![CDATA[mysterio]]></dc:creator><pubDate>Mon, 15 Dec 2008 19:08:29 GMT</pubDate></item><item><title><![CDATA[Reply to Curiouses bei der Verwendung von Templates on Mon, 15 Dec 2008 19:15:32 GMT]]></title><description><![CDATA[<p>Compiler?</p>
<p>Mit VC++ 2008 funktioniert das einwandfrei. Kann gut sein, dass ein älterer Compiler hier Probleme hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1630938</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1630938</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Mon, 15 Dec 2008 19:15:32 GMT</pubDate></item><item><title><![CDATA[Reply to Curiouses bei der Verwendung von Templates on Mon, 15 Dec 2008 19:23:29 GMT]]></title><description><![CDATA[<p>Naja, dies freut mich zwar soweit, dass der Code scheinbar richtig ist, aber ...</p>
<p>Ich verwende den g++ in der Version 4.3.2 unter Linux. Wie es aussieht scheint dies wohl ein g++-Bug zu sein. Kann dies jemand bestaetigen? Wenn ja, werde ich mal einen Bugzilla-Eintrag beim gcc machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1630942</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1630942</guid><dc:creator><![CDATA[mysterio]]></dc:creator><pubDate>Mon, 15 Dec 2008 19:23:29 GMT</pubDate></item><item><title><![CDATA[Reply to Curiouses bei der Verwendung von Templates on Mon, 15 Dec 2008 20:24:27 GMT]]></title><description><![CDATA[<p>Kein Bug.Mach mal so:</p>
<pre><code class="language-cpp">B() {
    int i=5;
    TypeT *_t=new TypeT();
    _t-&gt;callback.template bind&lt;&amp;::cb&gt;();
    _t-&gt;callback(i);
    _t-&gt;callback.template bind&lt;B,&amp;B::cb&gt;(this);
    _t-&gt;callback(i);
}
</code></pre>
<p>Wenn _t ein Templatetyp ist, musst du dem compiler gesondert mitteilen, wenn du planst,über ihn eine Templatemethode aufzurufen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1630963</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1630963</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Mon, 15 Dec 2008 20:24:27 GMT</pubDate></item><item><title><![CDATA[Reply to Curiouses bei der Verwendung von Templates on Mon, 15 Dec 2008 20:57:13 GMT]]></title><description><![CDATA[<p>Ja, dies funktioniert. Aber so richtig verstehe ich es nicht, muss ich zugeben. Kannst Du dies noch ein bisschen erklaeren bzw. wo findet man dies entsprechend im Standard?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1630980</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1630980</guid><dc:creator><![CDATA[mysterio]]></dc:creator><pubDate>Mon, 15 Dec 2008 20:57:13 GMT</pubDate></item><item><title><![CDATA[Reply to Curiouses bei der Verwendung von Templates on Mon, 15 Dec 2008 21:45:41 GMT]]></title><description><![CDATA[<p>Das ist recht einfach zu verstehen:</p>
<p>Die Syntax ist an der Stelle Kontextabhängig. &quot;bind&quot; muss keine Funktion sein, sondern ist vielleicht eine normale Variable.</p>
<p>Wenn der Compiler also etwas liest wie:</p>
<pre><code class="language-cpp">_t-&gt;callback.bind&lt;&amp;::cb&gt;()
</code></pre>
<p>kann er zum Zeitpunkt des parsen nicht heraus bekommen, was &quot;bind&quot; überhaupt ist, und kann demzufolge nicht wissen, ob das &quot;&lt;&quot; Zeichen ein operator&lt; oder eine template Klammer darstellt. Der Compiler kann zwar weiter lesen, und könnte dann bei dem Ausdruck &quot;&gt;()&quot; auf die Idee kommen, dass das ganze ein Funktionsaufruf sein könnte, aber es könnte auch ein einfacher Sytaxfehler sein.Deswegen hat man sich beim Standardcommitee dazu entschlossen(nein, ich hab den Standard nicht hier), dass man es den Compilerbauern einfacher macht, und deswegen vorschreibt, dass bind nur dann eine Templatemethode sein kann, wenn dies durch ein &quot;template&quot; angekündigt wurde.</p>
<p>Anschaulicher wirds vielleicht an diesem Beispiel:</p>
<pre><code class="language-cpp">_t.foo&lt;5&gt;(3+6);
</code></pre>
<p>Ohne zu wissen, was foo ist, kann man nicht zwischen einem Methodenraufruf mit einer Zahl als Templateparameter und dem hier unterscheiden:</p>
<pre><code class="language-cpp">(_t.foo&lt;5)&gt;3+6
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1631008</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1631008</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Mon, 15 Dec 2008 21:45:41 GMT</pubDate></item><item><title><![CDATA[Reply to Curiouses bei der Verwendung von Templates on Mon, 15 Dec 2008 21:50:27 GMT]]></title><description><![CDATA[<p>Jetzt hat es klick gemacht. Danke!!! man lernt wohl wirklich nie aus ...</p>
<p>Deine Signatur spricht uebrigens fuer sich und trifft hier wirklich absolut zu.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1631010</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1631010</guid><dc:creator><![CDATA[mysterio]]></dc:creator><pubDate>Mon, 15 Dec 2008 21:50:27 GMT</pubDate></item></channel></rss>