<?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[Template mit Funktionszeiger ohne Parameter-Typ]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>Ich steh gerade auf dem Schlauch oder ich hab schon lange kein C++ mehr geproggt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /><br />
Ich habe folgendes (stark reduziertes) Konstrukt:</p>
<pre><code class="language-cpp">class MyParamA{
};

class MyObjectA{
public:
        const MyParamA&amp; getParameter() const{
                return this-&gt;param;
        }

private:
        MyParamA param;
};

double fA(const MyParamA&amp;){
        return 0;
}

template&lt;double (*TParam)(const MyParamA&amp;)&gt;
class MyTemplate{
public:

template&lt;typename T&gt;
void doSomething(const T&amp; t){
        TParam(t.getParameter());
}

};

int main(int argc, char**)
{
        MyTemplate&lt;&amp;fA&gt; mt;
        MyObjectA moa;
        mt.doSomething(moa);
}
</code></pre>
<p>Das funktioniert ja auch wie ich es möchte.<br />
Jetzt hab ich analog dazu:</p>
<pre><code class="language-cpp">class MyParamB{
};

class MyObjectB{
public:
        const MyParamB&amp; getParameter() const;
};

double fB(const MyParamB&amp;);
</code></pre>
<p>Wie kann ich diese Definition ändern</p>
<pre><code class="language-cpp">template&lt;double (*TParam)(const MyParamA&amp;)&gt;
class MyTemplate{
        // ...
};
</code></pre>
<p>ohne einen zusätzlichen Template-Parameter für die Klasse zu ergänzen.<br />
Also theoretisch sollte es doch möglich sein, oder?<br />
Der Compiler hat doch alle Informationen.</p>
<p>Gruß,<br />
XSpille</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/278684/template-mit-funktionszeiger-ohne-parameter-typ</link><generator>RSS for Node</generator><lastBuildDate>Tue, 25 Aug 2026 02:15:50 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/278684.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 11 Dec 2010 00:29:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Template mit Funktionszeiger ohne Parameter-Typ on Sat, 11 Dec 2010 00:29:27 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>Ich steh gerade auf dem Schlauch oder ich hab schon lange kein C++ mehr geproggt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /><br />
Ich habe folgendes (stark reduziertes) Konstrukt:</p>
<pre><code class="language-cpp">class MyParamA{
};

class MyObjectA{
public:
        const MyParamA&amp; getParameter() const{
                return this-&gt;param;
        }

private:
        MyParamA param;
};

double fA(const MyParamA&amp;){
        return 0;
}

template&lt;double (*TParam)(const MyParamA&amp;)&gt;
class MyTemplate{
public:

template&lt;typename T&gt;
void doSomething(const T&amp; t){
        TParam(t.getParameter());
}

};

int main(int argc, char**)
{
        MyTemplate&lt;&amp;fA&gt; mt;
        MyObjectA moa;
        mt.doSomething(moa);
}
</code></pre>
<p>Das funktioniert ja auch wie ich es möchte.<br />
Jetzt hab ich analog dazu:</p>
<pre><code class="language-cpp">class MyParamB{
};

class MyObjectB{
public:
        const MyParamB&amp; getParameter() const;
};

double fB(const MyParamB&amp;);
</code></pre>
<p>Wie kann ich diese Definition ändern</p>
<pre><code class="language-cpp">template&lt;double (*TParam)(const MyParamA&amp;)&gt;
class MyTemplate{
        // ...
};
</code></pre>
<p>ohne einen zusätzlichen Template-Parameter für die Klasse zu ergänzen.<br />
Also theoretisch sollte es doch möglich sein, oder?<br />
Der Compiler hat doch alle Informationen.</p>
<p>Gruß,<br />
XSpille</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1992840</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1992840</guid><dc:creator><![CDATA[XSpille]]></dc:creator><pubDate>Sat, 11 Dec 2010 00:29:27 GMT</pubDate></item><item><title><![CDATA[Reply to Template mit Funktionszeiger ohne Parameter-Typ on Sat, 11 Dec 2010 01:22:19 GMT]]></title><description><![CDATA[<p>Ne, geht nicht.<br />
Es gibt keinen Template-Parameter Typ der zugleich ein Funktions-Typ UND ein Funktions-Zeiger wäre.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1992847</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1992847</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sat, 11 Dec 2010 01:22:19 GMT</pubDate></item><item><title><![CDATA[Reply to Template mit Funktionszeiger ohne Parameter-Typ on Sat, 11 Dec 2010 04:35:21 GMT]]></title><description><![CDATA[<p>Ich danke dir vielmals hustbaer!!!</p>
<p>Ich überlege, ob ich es nun etwa so machen soll:</p>
<pre><code class="language-cpp">template&lt;typename TFunction&gt;
class MyTemplate{
public:

template&lt;typename T&gt;
void doSomething(const T&amp; t){
        typedef typename TFunction::function_type function_type;
        TFunction::function(t.getParameter());
}

};

template &lt;typename TReturnType, typename TParamType, TReturnType (*TFunc)(TParamType)&gt;
struct FuncWrapper{
        typedef TReturnType (*function_type)(TParamType);
        static function_type function;
};

template &lt;typename TReturnType, typename TParamType, TReturnType (*TFunc)(TParamType)&gt;
TReturnType (*FuncWrapper&lt;TReturnType, TParamType, TFunc&gt;::function)(TParamType) = TFunc;

int main(int argc, char**) 
{
        typedef FuncWrapper&lt;double, const MyParamA&amp;, &amp;fA&gt; my_func;
        MyTemplate&lt;my_func&gt; mt;
        MyObjectA moa;
        mt.doSomething(moa);
}
</code></pre>
<p>Gibt es sowas evtl. schon in boost o. ä. integriert?<br />
Kann man die Infos evtl. auch aus boost::function wieder ermitteln?<br />
Ich hab im leider bisher nichts gefunden.</p>
<p>EDIT: Okay... Es ist spät... Aus der Template-Definition von boost::function wird man es<br />
natürlich nicht erhalten, da die Funktion ja Bestandteil eines spezifischen Objektes ist <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
<p>EDIT2:</p>
<p>Alternativ ginge auch Folgendes:</p>
<pre><code class="language-cpp">template&lt;typename T&gt;
void doSomething(const T&amp; t){
        TFunction::func(t.getParameter());
}

};

template &lt;typename TReturnType, typename TParamType, TReturnType (*TFunc)(TParamType)&gt;
struct FuncWrapper{
        static TReturnType func(TParamType param);
};

template &lt;typename TReturnType, typename TParamType, TReturnType (*TFunc)(TParamType)&gt;
TReturnType FuncWrapper&lt;TReturnType, TParamType, TFunc&gt;::func(TParamType param){
        TFunc(param);
}
</code></pre>
<p>Welche der beiden Möglichkeiten würdet ihr weswegen bevorzugen?</p>
<p>Kann man eigentlich auch etwas statisches wie</p>
<pre><code class="language-cpp">static TReturnType operator()(TParamType param);
</code></pre>
<p>(so klappts leider nicht) oder ähnlich implementieren, damit man</p>
<pre><code class="language-cpp">TFunction(t.getParameter());
</code></pre>
<p>aufrufen kann <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>Gruß,<br />
XSpille</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1992852</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1992852</guid><dc:creator><![CDATA[XSpille]]></dc:creator><pubDate>Sat, 11 Dec 2010 04:35:21 GMT</pubDate></item></channel></rss>