<?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[Templates + Stringliterale]]></title><description><![CDATA[<p>Man kennt das, ein Template ist vom Zeichentyp abhängig. In der Definition mit Literalen zu arbeiten, macht sich dann nicht so schön. Ich habe ein wenig rumgebastelt:</p>
<p>Eine Templatevariante:</p>
<pre><code class="language-cpp">literal&lt;char&gt;(&quot;bla&quot;)::value
literal&lt;wchar_t&gt;(&quot;bla&quot;)::value
</code></pre>
<p>Lässt sich leicht handhaben, kostet aber für wchar_t zur Laufzeit.</p>
<p>Dann hatte ich noch nen Makro, was aber immer eine Singlebyte und Multibyte Variante anlegt.</p>
<p>Wie geht ihr damit um?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/314145/templates-stringliterale</link><generator>RSS for Node</generator><lastBuildDate>Sat, 01 Aug 2026 09:48:32 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/314145.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 21 Feb 2013 19:32:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Templates + Stringliterale on Thu, 21 Feb 2013 19:32:54 GMT]]></title><description><![CDATA[<p>Man kennt das, ein Template ist vom Zeichentyp abhängig. In der Definition mit Literalen zu arbeiten, macht sich dann nicht so schön. Ich habe ein wenig rumgebastelt:</p>
<p>Eine Templatevariante:</p>
<pre><code class="language-cpp">literal&lt;char&gt;(&quot;bla&quot;)::value
literal&lt;wchar_t&gt;(&quot;bla&quot;)::value
</code></pre>
<p>Lässt sich leicht handhaben, kostet aber für wchar_t zur Laufzeit.</p>
<p>Dann hatte ich noch nen Makro, was aber immer eine Singlebyte und Multibyte Variante anlegt.</p>
<p>Wie geht ihr damit um?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2300778</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2300778</guid><dc:creator><![CDATA[bunga bunga]]></dc:creator><pubDate>Thu, 21 Feb 2013 19:32:54 GMT</pubDate></item><item><title><![CDATA[Reply to Templates + Stringliterale on Thu, 21 Feb 2013 19:41:50 GMT]]></title><description><![CDATA[<p>bunga bunga schrieb:</p>
<blockquote>
<p>Wie geht ihr damit um?</p>
</blockquote>
<p>Wofür genau brauchst du sowas?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2300782</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2300782</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Thu, 21 Feb 2013 19:41:50 GMT</pubDate></item><item><title><![CDATA[Reply to Templates + Stringliterale on Thu, 21 Feb 2013 19:50:21 GMT]]></title><description><![CDATA[<p>dot schrieb:</p>
<blockquote>
<p>bunga bunga schrieb:</p>
<blockquote>
<p>Wie geht ihr damit um?</p>
</blockquote>
<p>Wofür genau brauchst du sowas?</p>
</blockquote>
<p>Eigentlich nur aus Bequemlichkeit, weil ich nicht spezialisieren möchte. Die Implementation ist identisch, außer das die Literale angepasst werden müssen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2300786</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2300786</guid><dc:creator><![CDATA[bunga bunga]]></dc:creator><pubDate>Thu, 21 Feb 2013 19:50:21 GMT</pubDate></item><item><title><![CDATA[Reply to Templates + Stringliterale on Thu, 21 Feb 2013 19:51:33 GMT]]></title><description><![CDATA[<p>Ich kann mir trotzdem einfach nicht vorstellen, wofür man sowas brauchen sollte. Kannst du mir ein Anwendungsbeispiel geben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2300787</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2300787</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Thu, 21 Feb 2013 19:51:33 GMT</pubDate></item><item><title><![CDATA[Reply to Templates + Stringliterale on Thu, 21 Feb 2013 20:13:43 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>Nathan schrieb:</p>
<blockquote>
<p>Es geht sogar viel einfach wie ich gerade feststellen konnte.</p>
<pre><code>string literal = &quot;Das Literal, immer vom Typ string.&quot;;
funktionDieLiteralBraucht(basic_string&lt;E, Traits&gt;(literal.begin(), literal.end()));
</code></pre>
<p>Klappt bei mir (gcc und C++11) wunderbar.</p>
</blockquote>
<p>Gute Idee, danke <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>
<pre><code class="language-cpp">template &lt;class E, class T = std::char_traits&lt;E&gt;, unsigned N = 0&gt;
std::basic_string&lt;E, T&gt; convertLiteral(char const(&amp; lit)[N])
{ return std::basic_string&lt;E, T&gt;(lit, lit+N); }

template&lt; typename E, typename Traits &gt;
std::basic_string&lt; E, Traits &gt; replaceElements( std::basic_string&lt; E, Traits &gt; str ) {
    auto tag = convertLiteral&lt;E, Traits&gt;(&quot;Element&quot;);
    auto pos = str.find(tag);
    if (pos != decltype(str)::npos)
        str.replace(pos, tag.length(), convertLiteral&lt;E, Traits&gt;(&quot;Tnemele&quot;));
    return str;
}
 
template &lt;typename E&gt;
auto replaceElements(E const* ptr) -&gt; std::basic_string&lt;E&gt;
{ return replaceElements(std::basic_string&lt;E&gt;(ptr)); }
</code></pre>
<p>Trotzdem kein wchar_t bitte <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_tongue"
      title=":P"
      alt="😛"
    /></p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2300796</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2300796</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Thu, 21 Feb 2013 20:13:43 GMT</pubDate></item><item><title><![CDATA[Reply to Templates + Stringliterale on Thu, 21 Feb 2013 20:16:55 GMT]]></title><description><![CDATA[<p>Wäre doch eigentlich überall ganz praktisch, wo es um Ein/Ausgabe geht.</p>
<p>Beispielsweise wenn der Ausgabeoperator überladen werden soll:</p>
<pre><code class="language-cpp">template &lt;typename charT&gt;
std::basic_ostream&lt;charT&gt;&amp; operator &lt;&lt;(std::basic_ostream&lt;charT&gt; s, const Foo&amp; f)
{
    s &lt;&lt; literal_cast&lt;charT&gt;(&quot;bar() = &quot;) &lt;&lt; f.bar() &lt;&lt; literal_cast&lt;charT&gt;(&quot;baz() = &quot;) &lt;&lt; f.baz();
}
</code></pre>
<p>Bei der Instanziierung werden dann einfach die richtigen Adressen eingesetz und fertig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2300800</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2300800</guid><dc:creator><![CDATA[bunga bunga]]></dc:creator><pubDate>Thu, 21 Feb 2013 20:16:55 GMT</pubDate></item><item><title><![CDATA[Reply to Templates + Stringliterale on Thu, 21 Feb 2013 20:51:40 GMT]]></title><description><![CDATA[<p>Wenn du dich damit abfinden kannst</p>
<pre><code class="language-cpp">'b','a','r','(',')',' ','=',' '
</code></pre>
<p>zu schreiben gibt es einige schöne Lösungen. Ansonsten eher nicht.</p>
<p>Das String-Handling zur Compile-Zeit finde ich echt schwach gelöst von C++11.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/29873">@Nathan</a>: Inwiefern kostet das nicht zur Laufzeit?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2300814</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2300814</guid><dc:creator><![CDATA[gtmarg]]></dc:creator><pubDate>Thu, 21 Feb 2013 20:51:40 GMT</pubDate></item><item><title><![CDATA[Reply to Templates + Stringliterale on Thu, 21 Feb 2013 20:51:51 GMT]]></title><description><![CDATA[<p>Danek schonmal, an string hatte ich gar nicht gedacht.</p>
<p>Wie kann das funktionieren?</p>
<pre><code class="language-cpp">auto tag = convertLiteral&lt;E, Traits&gt;(&quot;Element&quot;);
</code></pre>
<p>tag müsste doch eigenlich leer sein?!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2300815</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2300815</guid><dc:creator><![CDATA[bunga bunga]]></dc:creator><pubDate>Thu, 21 Feb 2013 20:51:51 GMT</pubDate></item><item><title><![CDATA[Reply to Templates + Stringliterale on Thu, 21 Feb 2013 20:58:18 GMT]]></title><description><![CDATA[<p>Das ist C++11, google mal auto.</p>
<p>Und die Laufzeitkosten sind dank inline und move quasi null.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2300819</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2300819</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Thu, 21 Feb 2013 20:58:18 GMT</pubDate></item><item><title><![CDATA[Reply to Templates + Stringliterale on Thu, 21 Feb 2013 21:43:32 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/29873">@Nathan</a>: Mit move hat das rein gar nichts zu tun, der Punkt ist halt, dass bei dir das String-Literal als char[] im Datensegment liegt und dann zur Laufzeit darüberiteriert werden muss um jedes Zeichen in wchar_t zu konvertieren.</p>
<p>Ich habe hier eine Version, bei der ein wchar_t[] im Datensegment abgelegt wird:</p>
<pre><code class="language-cpp">// &lt;kopiert author=&quot;camper&quot;&gt;
template &lt;typename T&gt; struct identity_ { using type = T; };
template &lt;int... i&gt; struct index_list : identity_&lt;index_list&lt;i...&gt;&gt; {};
template &lt;int N, typename = index_list&lt;&gt;&gt; struct make_index_list_;
template &lt;int N, typename T = index_list&lt;&gt;&gt;
using make_index_list = typename make_index_list_&lt;N, T&gt;::type;
template &lt;int N, int... i&gt;
struct make_index_list_&lt;N, index_list&lt;i...&gt;&gt;
  : make_index_list&lt;N-1, index_list&lt;0, 1+i...&gt;&gt; {};
template &lt;int... i&gt;
struct make_index_list_&lt;0, index_list&lt;i...&gt;&gt;
  : index_list&lt;i...&gt; {};
// &lt;/kopiert&gt;

template &lt;typename C, unsigned N&gt;
struct string_lit { C data[N]; };

template &lt;class C, unsigned N, int... I&gt;
constexpr string_lit&lt;C, N&gt; convert_literal_(char const(&amp; lit)[N],
                                            index_list&lt;I...&gt;)
{
  return {{ static_cast&lt;C&gt;(lit[I])... }};
}

template &lt;class C, unsigned N = 0&gt;
constexpr string_lit&lt;C, N&gt; convert_literal(char const(&amp; lit)[N])
{
  return convert_literal_&lt;C, N&gt;(lit, make_index_list&lt;N&gt;{});
}

void gtmarg()
{
  constexpr auto lit = convert_literal&lt;wchar_t&gt;(&quot;hello world, this is a very very very long string and it has to be stored in a very very constant array&quot;);
  printme(lit.data);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2300828</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2300828</guid><dc:creator><![CDATA[gtmarg]]></dc:creator><pubDate>Thu, 21 Feb 2013 21:43:32 GMT</pubDate></item><item><title><![CDATA[Reply to Templates + Stringliterale on Sat, 23 Feb 2013 23:07:05 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Das ist C++11, google mal auto.</p>
</blockquote>
<p>Auto hat damit gar nichts zu tun, ich war überrascht, dass man die Größe des Arrays einfach über das Template-Pattern abgreifen kann und sie nicht explizit angeben muss.</p>
<p>@gtmarg + camper<br />
Danke, genau das was ich suche. Leider noch zuviel C++11 für den VC11. Ich habs vorübergehend erstmal so gelöst:</p>
<pre><code>template &lt;typename CharT&gt;
struct string_literal;

template &lt;&gt;
struct string_literal&lt;char&gt;
{
	string_literal(const char* lit) : data(lit) {}
	const char* data;
	inline const char* c_str() {return data;}
};

template &lt;&gt;
struct string_literal&lt;wchar_t&gt;
{
	template &lt;std::size_t SizeV&gt;
	string_literal(char const(&amp; lit)[SizeV]) : data(lit, lit + SizeV - 1) {}
	std::wstring data;
	inline const wchar_t* c_str() {return data.c_str();}
};

template &lt;typename CharT&gt;
inline std::basic_ostream&lt;CharT&gt;&amp; operator&lt;&lt;(std::basic_ostream&lt;CharT&gt;&amp; s, const string_literal&lt;CharT&gt;&amp; l)
{
	return s &lt;&lt; l.data;
}
</code></pre>
<p>Btw, macht es sind mit</p>
<pre><code>#if __cplusplus &gt;= 201103L
</code></pre>
<p>den Standard zu prüfen?</p>
<p>MfG</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2301492</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2301492</guid><dc:creator><![CDATA[bunga bunga]]></dc:creator><pubDate>Sat, 23 Feb 2013 23:07:05 GMT</pubDate></item></channel></rss>