<?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 typ als string]]></title><description><![CDATA[<p>Hi gibts eine Möglichkeit den Templateparameter einer Funktion als String zu bekommen:</p>
<pre><code class="language-cpp">template&lt;class T&gt;
void testFunc(T i)
{
  std::cout &lt;&lt; &quot;this is a specialization of testFunc &quot; 
            &lt;&lt; &quot;with Type '&quot;&lt;&lt; T.asString  &lt;&lt;&quot;'&quot;
            &lt;&lt; &quot;value=&quot;&lt;&lt;i&lt;&lt;std::endl;
}
</code></pre>
<p>Gruß<br />
Vlad</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/299739/template-typ-als-string</link><generator>RSS for Node</generator><lastBuildDate>Thu, 13 Aug 2026 02:29:35 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/299739.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 17 Feb 2012 09:46:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to template typ als string on Fri, 17 Feb 2012 09:46:23 GMT]]></title><description><![CDATA[<p>Hi gibts eine Möglichkeit den Templateparameter einer Funktion als String zu bekommen:</p>
<pre><code class="language-cpp">template&lt;class T&gt;
void testFunc(T i)
{
  std::cout &lt;&lt; &quot;this is a specialization of testFunc &quot; 
            &lt;&lt; &quot;with Type '&quot;&lt;&lt; T.asString  &lt;&lt;&quot;'&quot;
            &lt;&lt; &quot;value=&quot;&lt;&lt;i&lt;&lt;std::endl;
}
</code></pre>
<p>Gruß<br />
Vlad</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2182206</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2182206</guid><dc:creator><![CDATA[vlad_tepesch]]></dc:creator><pubDate>Fri, 17 Feb 2012 09:46:23 GMT</pubDate></item><item><title><![CDATA[Reply to template typ als string on Fri, 17 Feb 2012 10:26:07 GMT]]></title><description><![CDATA[<p>Was genau soll da zurückkommen?<br />
Wie wärs mit sowas:</p>
<pre><code class="language-cpp">template &lt;typename T&gt;
struct as_string;

template &lt;&gt;
struct as_string&lt;int&gt;
{
  static const char value[];
};
template &lt;&gt;
const char as_string&lt;int&gt;::value[] = &quot;int&quot;;

template &lt;&gt;
struct as_string&lt;float&gt;
{
  static const char value[];
};
template &lt;&gt;
const char as_string&lt;float&gt;::value[] = &quot;float&quot;;

...
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2182218</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2182218</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Fri, 17 Feb 2012 10:26:07 GMT</pubDate></item><item><title><![CDATA[Reply to template typ als string on Fri, 17 Feb 2012 10:30:09 GMT]]></title><description><![CDATA[<blockquote>
<p>Was genau soll da zurückkommen?</p>
</blockquote>
<p>eigentlich genau das, was man als T angibt.</p>
<blockquote>
<p>Wie wärs mit sowas:</p>
</blockquote>
<p>gut, nach dem schema wäre es als spezialisierte template funktion weniger schreibarbeit:</p>
<pre><code class="language-cpp">template&lt;typename T&gt; const char* toString(){ return &quot;unknown&quot;;}

template&lt;&gt; const char* toString&lt;int&gt;(){ return &quot;int&quot;;}
template&lt;&gt; const char* toString&lt;double&gt;(){ return &quot;double&quot;;}

// [...]
  std::cout &lt;&lt; &quot;with Type '&quot;&lt;&lt; toString&lt;T&gt;() &lt;&lt;&quot;'&quot;;
</code></pre>
<p>arbeit ist es trotzdem und man muss es manuell erweitern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2182235</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2182235</guid><dc:creator><![CDATA[vlad_tepesch]]></dc:creator><pubDate>Fri, 17 Feb 2012 10:30:09 GMT</pubDate></item><item><title><![CDATA[Reply to template typ als string on Fri, 17 Feb 2012 10:46:21 GMT]]></title><description><![CDATA[<p>typeid(T).name()</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2182245</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2182245</guid><dc:creator><![CDATA[typeider]]></dc:creator><pubDate>Fri, 17 Feb 2012 10:46:21 GMT</pubDate></item><item><title><![CDATA[Reply to template typ als string on Fri, 17 Feb 2012 11:38:06 GMT]]></title><description><![CDATA[<p>danke, ich glaube, das habe ich gesucht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2182267</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2182267</guid><dc:creator><![CDATA[vlad_tepesch]]></dc:creator><pubDate>Fri, 17 Feb 2012 11:38:06 GMT</pubDate></item><item><title><![CDATA[Reply to template typ als string on Fri, 17 Feb 2012 11:48:29 GMT]]></title><description><![CDATA[<p>Es ist nicht definiert was typeid(bla).name() genau liefert, das ist völlig compilerabhängig...genau darum eben die Lösung über trait-artige templates.<br />
Die Version mit structs hat den Vorteil, dass du sie partiell spezialisieren kannst, z.B. um Pointertypen rekursiv zu behandeln. Zugegeben, mit const char* wird das wohl nicht so einach gehen, aber z.B. mit std::string schon...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2182269</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2182269</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Fri, 17 Feb 2012 11:48:29 GMT</pubDate></item><item><title><![CDATA[Reply to template typ als string on Fri, 17 Feb 2012 12:18:27 GMT]]></title><description><![CDATA[<p>Ich würde es wie dot vorgeschlagen machen. Mit ein paar Makros gewürzt wird das ganze dann auch besser lesbar.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

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

#define DEFINE_STRING_IMPL(pattern, val, ...) \
	template &lt;__VA_ARGS__&gt; \
	struct type_to_string&lt;pattern&gt; \
	{ \
		static std::string value() \
		{ \
			return val; \
		} \
	};

#define TO_STRING(type) type_to_string&lt;type&gt;::value()

#define DEFINE_STRING_0(pattern, value) DEFINE_STRING_IMPL(pattern, value)
#define DEFINE_STRING_1(pattern, value) DEFINE_STRING_IMPL(pattern, value, typename T)
#define DEFINE_STRING_2(pattern, value) DEFINE_STRING_IMPL(pattern, value, typename T, typename U)

DEFINE_STRING_0(int   , &quot;int&quot;)
DEFINE_STRING_1(T*    , TO_STRING(T) + &quot;*&quot;)
DEFINE_STRING_2(T U::*, TO_STRING(T) + &quot; &quot; + TO_STRING(U) + &quot;::*&quot;)

struct foo {};

DEFINE_STRING_0(foo, &quot;foo&quot;)

int main()
{
	std::cout &lt;&lt; TO_STRING(int foo::**);
}
</code></pre>
<p>Wenn ein Typ nicht bekannt sein sollte, gibts einen Compilezeit-Fehler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2182277</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2182277</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Fri, 17 Feb 2012 12:18:27 GMT</pubDate></item><item><title><![CDATA[Reply to template typ als string on Fri, 17 Feb 2012 12:51:28 GMT]]></title><description><![CDATA[<p>dot schrieb:</p>
<blockquote>
<p>Es ist nicht definiert was typeid(bla).name() genau liefert, das ist völlig compilerabhängig...</p>
</blockquote>
<p>Richtig.</p>
<blockquote>
<p>genau darum eben die Lösung über trait-artige templates.</p>
</blockquote>
<p>Naja, kommt drauf an was man braucht. Für Dinge wie Log-Ausgaben ist typeid(bla).name() oft vollkommen ausreichend.</p>
<blockquote>
<p>Die Version mit structs hat den Vorteil, dass du sie partiell spezialisieren kannst, z.B. um Pointertypen rekursiv zu behandeln. Zugegeben, mit const char* wird das wohl nicht so einach gehen, aber z.B. mit std::string schon...</p>
</blockquote>
<p>Oder auch indem man die C++ String-Builder Klasse verwendet, auch bekannt als std::vector&lt;char&gt; (allerdings dann als in/out Parameter, und nicht als Returnwert).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2182296</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2182296</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Fri, 17 Feb 2012 12:51:28 GMT</pubDate></item><item><title><![CDATA[Reply to template typ als string on Fri, 17 Feb 2012 13:02:24 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>dot schrieb:</p>
<blockquote>
<p>Es ist nicht definiert was typeid(bla).name() genau liefert, das ist völlig compilerabhängig...</p>
</blockquote>
<p>Richtig.</p>
<blockquote>
<p>genau darum eben die Lösung über trait-artige templates.</p>
</blockquote>
<p>Naja, kommt drauf an was man braucht. Für Dinge wie Log-Ausgaben ist typeid(bla).name() oft vollkommen ausreichend.</p>
</blockquote>
<p>Absolut, ich ging natürlich ursprünglich davon aus, dass es eben um die konsistente Ausgabe ging, da ja sonst typeid() verwendet würde <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>hustbaer schrieb:</p>
<blockquote>
<blockquote>
<p>Die Version mit structs hat den Vorteil, dass du sie partiell spezialisieren kannst, z.B. um Pointertypen rekursiv zu behandeln. Zugegeben, mit const char* wird das wohl nicht so einach gehen, aber z.B. mit std::string schon...</p>
</blockquote>
<p>Oder auch indem man die C++ String-Builder Klasse verwendet, auch bekannt als std::vector&lt;char&gt; (allerdings dann als in/out Parameter, und nicht als Returnwert).</p>
</blockquote>
<p>Stimmt, das wär vermutlich die bessere Lösung als der Rückgabewert, egal ob jetzt mit vector oder string.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2182303</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2182303</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Fri, 17 Feb 2012 13:02:24 GMT</pubDate></item><item><title><![CDATA[Reply to template typ als string on Fri, 17 Feb 2012 17:53:06 GMT]]></title><description><![CDATA[<p>also zu typeid(Type).name():<br />
In der jeweiligen Doku des Compilers sollte das durchaus spezifiziert sein <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";-)"
      alt="😉"
    /></p>
<p>MSVC gibt da wunderbare brauchbare Strings zurück ala &quot;int&quot;, &quot;class foo&quot; usw.<br />
GCC ist da etwas umständlicher, die nehmen Kürzel wie &quot;i&quot;, &quot;Ss&quot; (S = std, s = string) usw. (kann man durch Probieren auch herausfinden)</p>
<p>Also prinzipiell lässt sich damit Compiler-abhängig auch was bauen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2182415</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2182415</guid><dc:creator><![CDATA[DrakoXP]]></dc:creator><pubDate>Fri, 17 Feb 2012 17:53:06 GMT</pubDate></item><item><title><![CDATA[Reply to template typ als string on Fri, 17 Feb 2012 21:35:00 GMT]]></title><description><![CDATA[<p><a href="http://www.c-plusplus.net/forum/290867" rel="nofollow">Sowas</a> hatten wir glaube ich schon mal.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2182501</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2182501</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 17 Feb 2012 21:35:00 GMT</pubDate></item><item><title><![CDATA[Reply to template typ als string on Fri, 17 Feb 2012 22:53:59 GMT]]></title><description><![CDATA[<p>Mit C++11 und constexpr wird das Boost.MPL-Zeugs dort hinfällig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2182520</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2182520</guid><dc:creator><![CDATA[konschdeksbr]]></dc:creator><pubDate>Fri, 17 Feb 2012 22:53:59 GMT</pubDate></item></channel></rss>