<?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[Klassen template methode Rückgabewert abhängig von Templatetyp]]></title><description><![CDATA[<p>Ich brauche zwei Klassen die sich nur in zwei wesentlichen Dingen unterscheiden: der Typ eines Members und der enum-wert den eine Methode zurückgibt. Ich würde das gerne als Template realisieren um massive Codeduplikation zu vermeiden. Den Member Typ als Template-Parameter festzulegen ist kein Problem, aber wie realisiere ich es, dass die Funktion abhängig vom Membertyp den richtigen enum-Wert zurückliefert, wobei diese Auswertung am besten noch zur Kompilezeit geschehen soll. Hat jemand eine Idee?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/324375/klassen-template-methode-rückgabewert-abhängig-von-templatetyp</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Jul 2026 10:29:48 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/324375.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 14 Mar 2014 12:27:03 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Klassen template methode Rückgabewert abhängig von Templatetyp on Fri, 14 Mar 2014 12:27:03 GMT]]></title><description><![CDATA[<p>Ich brauche zwei Klassen die sich nur in zwei wesentlichen Dingen unterscheiden: der Typ eines Members und der enum-wert den eine Methode zurückgibt. Ich würde das gerne als Template realisieren um massive Codeduplikation zu vermeiden. Den Member Typ als Template-Parameter festzulegen ist kein Problem, aber wie realisiere ich es, dass die Funktion abhängig vom Membertyp den richtigen enum-Wert zurückliefert, wobei diese Auswertung am besten noch zur Kompilezeit geschehen soll. Hat jemand eine Idee?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2388873</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2388873</guid><dc:creator><![CDATA[TNA]]></dc:creator><pubDate>Fri, 14 Mar 2014 12:27:03 GMT</pubDate></item><item><title><![CDATA[Reply to Klassen template methode Rückgabewert abhängig von Templatetyp on Fri, 14 Mar 2014 12:35:45 GMT]]></title><description><![CDATA[<p>also existiert eine injektion von den template-typen in die enum-werte?</p>
<p>dann kannst du doch sowas machen (ungetestet und aus dem stehgreif):</p>
<pre><code>enum Types // Oder Safe-Enums mit C++11 bzw. Safe-Enum-Idiom in C++03
{
	tBool,
	tInt
};

template&lt;typename T&gt;
struct Injection;

template&lt;&gt;
struct Injection&lt;bool&gt;
{
	static const Types Value = tBool;
};

template&lt;&gt;
struct Injection&lt;int&gt;
{
	static const Types Value = tInt;
};

// Oder allgemein gegen diesen Boilerplate-Code:
#define MyLib_MakeInjection(TypeName, EnumName) \
template&lt;&gt; \
struct Injection&lt;TypeName&gt; \
{ \
	static const Types Value = EnumName \
};

template&lt;typename T, template&lt;class&gt; class MyInjection = Injection&gt;
struct MyClass
{
	T MyData;
	Types Type() const
	{
		return MyInjection&lt;T&gt;::Value;
	}
};
</code></pre>
<p>edit: falscher kasus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2388875</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2388875</guid><dc:creator><![CDATA[Fytch]]></dc:creator><pubDate>Fri, 14 Mar 2014 12:35:45 GMT</pubDate></item><item><title><![CDATA[Reply to Klassen template methode Rückgabewert abhängig von Templatetyp on Fri, 14 Mar 2014 12:42:19 GMT]]></title><description><![CDATA[<p>Danke, ich denke so sollte es funktionieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2388877</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2388877</guid><dc:creator><![CDATA[TNA]]></dc:creator><pubDate>Fri, 14 Mar 2014 12:42:19 GMT</pubDate></item><item><title><![CDATA[Reply to Klassen template methode Rückgabewert abhängig von Templatetyp on Fri, 14 Mar 2014 12:50:06 GMT]]></title><description><![CDATA[<blockquote>
<p>Ich würde das gerne als Template realisieren</p>
</blockquote>
<p>Du kannst auch private von einer gemeinsamen Basisklasse erben, du kannst die Memberfunktion spezialisieren, du kannst .... auch wild mit defines arbeiten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2388880</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2388880</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Fri, 14 Mar 2014 12:50:06 GMT</pubDate></item><item><title><![CDATA[Reply to Klassen template methode Rückgabewert abhängig von Templatetyp on Fri, 14 Mar 2014 14:37:57 GMT]]></title><description><![CDATA[<p>Funktioniert super mit der injection. Nun habe ich in eine Methode noch die Anforderung, abhängig vom typename T eine Zeichenkette auszugeben. Da habe ich noch keine Idee wie das laufen soll. Meine Versuche eine const char[] in die Injection zu packen, haben leider nicht funktioniert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2388913</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2388913</guid><dc:creator><![CDATA[TNA]]></dc:creator><pubDate>Fri, 14 Mar 2014 14:37:57 GMT</pubDate></item><item><title><![CDATA[Reply to Klassen template methode Rückgabewert abhängig von Templatetyp on Fri, 14 Mar 2014 14:45:31 GMT]]></title><description><![CDATA[<p>entweder mit meinem alten ansatz:</p>
<pre><code>template&lt;typename T&gt;
struct Injection;

template&lt;&gt;
struct Injection&lt;bool&gt;
{
    static char const* const Value;
};
template&lt;&gt;
char const* const Injection&lt;bool&gt;::Value = &quot;bool&quot;;

template&lt;&gt;
struct Injection&lt;int&gt;
{
    static char const* const Value;
};
template&lt;&gt;
char const* const Injection&lt;int&gt;::Value = &quot;int&quot;;

// Komfort:
#define MyLib_MakeInjection(TypeName) \
template&lt;&gt; \
struct Injection&lt;TypeName&gt; \
{ \
    static char const* const Value; \
}; \
template&lt;&gt; \
char const* const Injection&lt;TypeName&gt;::Value = #TypeName;

template&lt;typename T, template&lt;class&gt; class MyInjection = Injection&gt;
struct MyClass
{
    T MyData;
    char const* Name() const
    {
        return MyInjection&lt;T&gt;::Value;
    }
};
</code></pre>
<p>oder vererbung:</p>
<pre><code>template&lt;typename T&gt;
struct TypeNameCStr;

template&lt;&gt;
struct TypeNameCStr&lt;bool&gt;
{
	char const* Name() const
    {
        return &quot;bool&quot;;
    }
};

template&lt;&gt;
struct TypeNameCStr&lt;int&gt;
{
	char const* Name() const
    {
        return &quot;int&quot;;
    }
};

// Komfort:
#define MyLib_MakeInjection(TypeName) \
template&lt;&gt; \
struct TypeNameCStr&lt;TypeName&gt; \
{ \
	char const* Name() const \
    { \
        return #TypeName; \
    } \
};

MyLib_MakeInjection(float)
MyLib_MakeInjection(char)

template&lt;typename T&gt;
struct MyClass : public TypeNameCStr&lt;T&gt;
{
    // ...
};
</code></pre>
<p>ich weiss gar nicht was ihr gegen makros habt, die das ganze wesentlich leserlicher machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2388919</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2388919</guid><dc:creator><![CDATA[Fytch]]></dc:creator><pubDate>Fri, 14 Mar 2014 14:45:31 GMT</pubDate></item><item><title><![CDATA[Reply to Klassen template methode Rückgabewert abhängig von Templatetyp on Fri, 14 Mar 2014 15:01:29 GMT]]></title><description><![CDATA[<pre><code>template&lt;&gt; 
struct Injection&lt;int&gt; 
{ 
    static char const* const Value; 
}; 
template&lt;&gt; 
char const* const Injection&lt;int&gt;::Value = &quot;int&quot;;
</code></pre>
<p>Gibt ein &quot;template header not allowed in member definition of explicitly specialized class&quot;</p>
<p>Wenn ich es ändere in</p>
<pre><code>template&lt;&gt; 
struct Injection&lt;int&gt; 
{ 
    static char const* const Value; 
}; 
char const* const Injection&lt;int&gt;::Value = &quot;int&quot;;
</code></pre>
<p>Kompiliert es zwar, aber ich bekome ein &quot;multiple definition of Injection&lt;int&gt;::Value&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2388921</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2388921</guid><dc:creator><![CDATA[TNA]]></dc:creator><pubDate>Fri, 14 Mar 2014 15:01:29 GMT</pubDate></item><item><title><![CDATA[Reply to Klassen template methode Rückgabewert abhängig von Templatetyp on Fri, 14 Mar 2014 15:04:24 GMT]]></title><description><![CDATA[<p>Das</p>
<pre><code>char const* const Injection&lt;int&gt;::Value = &quot;int&quot;;
</code></pre>
<p>darf nicht im Header stehen, sonst wird es in jeder Übersetzungseinheit, in der der Header eingebunden wird neu definiert</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2388922</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2388922</guid><dc:creator><![CDATA[patrick246]]></dc:creator><pubDate>Fri, 14 Mar 2014 15:04:24 GMT</pubDate></item></channel></rss>