<?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[Initialisierung von char* via template gibt Warnung [gelöst]]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe folgenden Code:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;type_traits&gt;

struct char_test
{
  // initialize const char*
  char_test(const char s[])
    : str(s)
  {}

  const char* str;
};

template &lt; class T, class Enabled = void &gt;
struct template_test;

template &lt; class T &gt;
struct template_test&lt;T, typename std::enable_if&lt; !std::is_same&lt;T, char*&gt;::value &gt;::type &gt;
{
  // initialize any type except char*
  explicit template_test(const T &amp;t) : value(t) {}
  const T value;
};

template &lt; class T &gt;
struct template_test&lt;T, typename std::enable_if&lt; std::is_same&lt;T, char*&gt;::value &gt;::type &gt;
{
  // initialize char*
  explicit template_test(const char s[]) : str(s) {}
  const T str;
};

int main()
{
    char_test c1(&quot;baba&quot;);
    template_test&lt;char*&gt;c2(&quot;baba&quot;);
    template_test&lt;int&gt; i1(7);

    std::cout &lt;&lt; &quot;c1: &quot; &lt;&lt; c1.str &lt;&lt; '\n';
    std::cout &lt;&lt; &quot;c2: &quot; &lt;&lt; c2.str &lt;&lt; '\n';
    std::cout &lt;&lt; &quot;i1: &quot; &lt;&lt; i1.value &lt;&lt; '\n';
    return 0;
}
</code></pre>
<p>Beim kompilieren (gcc 5.1 c++14) gibt's es für die template Variante mit char* folgende Warnung:</p>
<pre><code>main.cpp: In instantiation of ‘template_test&lt;T, typename std::enable_if&lt;std::is_same&lt;T, char*&gt;::value&gt;::type&gt;::template_test(const char*) [with T = char*; typename std::enable_if&lt;std::is_same&lt;T, char*&gt;::value&gt;::type = void]’:
main.cpp:37:37:   required from here
main.cpp:29:49: warning: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
   explicit template_test(const char s[]) : str(s) {}
</code></pre>
<p>Der Nicht-Template Code kompiliert ohne Warnung. Obwohl (meiner Meinung nach) die gleiche Klasse erzeugt wird.</p>
<p>Hat hier jemand vielleicht eine Erklärung für mich?</p>
<p>Besten Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/334904/initialisierung-von-char-via-template-gibt-warnung-gelöst</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Apr 2026 01:15:31 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/334904.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 19 Oct 2015 11:27:40 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Initialisierung von char* via template gibt Warnung [gelöst] on Mon, 19 Oct 2015 12:04:12 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe folgenden Code:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;type_traits&gt;

struct char_test
{
  // initialize const char*
  char_test(const char s[])
    : str(s)
  {}

  const char* str;
};

template &lt; class T, class Enabled = void &gt;
struct template_test;

template &lt; class T &gt;
struct template_test&lt;T, typename std::enable_if&lt; !std::is_same&lt;T, char*&gt;::value &gt;::type &gt;
{
  // initialize any type except char*
  explicit template_test(const T &amp;t) : value(t) {}
  const T value;
};

template &lt; class T &gt;
struct template_test&lt;T, typename std::enable_if&lt; std::is_same&lt;T, char*&gt;::value &gt;::type &gt;
{
  // initialize char*
  explicit template_test(const char s[]) : str(s) {}
  const T str;
};

int main()
{
    char_test c1(&quot;baba&quot;);
    template_test&lt;char*&gt;c2(&quot;baba&quot;);
    template_test&lt;int&gt; i1(7);

    std::cout &lt;&lt; &quot;c1: &quot; &lt;&lt; c1.str &lt;&lt; '\n';
    std::cout &lt;&lt; &quot;c2: &quot; &lt;&lt; c2.str &lt;&lt; '\n';
    std::cout &lt;&lt; &quot;i1: &quot; &lt;&lt; i1.value &lt;&lt; '\n';
    return 0;
}
</code></pre>
<p>Beim kompilieren (gcc 5.1 c++14) gibt's es für die template Variante mit char* folgende Warnung:</p>
<pre><code>main.cpp: In instantiation of ‘template_test&lt;T, typename std::enable_if&lt;std::is_same&lt;T, char*&gt;::value&gt;::type&gt;::template_test(const char*) [with T = char*; typename std::enable_if&lt;std::is_same&lt;T, char*&gt;::value&gt;::type = void]’:
main.cpp:37:37:   required from here
main.cpp:29:49: warning: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
   explicit template_test(const char s[]) : str(s) {}
</code></pre>
<p>Der Nicht-Template Code kompiliert ohne Warnung. Obwohl (meiner Meinung nach) die gleiche Klasse erzeugt wird.</p>
<p>Hat hier jemand vielleicht eine Erklärung für mich?</p>
<p>Besten Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2471600</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2471600</guid><dc:creator><![CDATA[zussel]]></dc:creator><pubDate>Mon, 19 Oct 2015 12:04:12 GMT</pubDate></item><item><title><![CDATA[Reply to Initialisierung von char* via template gibt Warnung [gelöst] on Mon, 19 Oct 2015 11:38:06 GMT]]></title><description><![CDATA[<p>Stringliterale sind vom Typ const char[] (array mit konstantem inhalt). Dein const T in template_test wird bei T=char* zu char* const (konstanter zeiger, aber das worauf er zeigt ist es nicht).<br />
Statt deinem std::enable_if gewurschtel kannst du auch einfach template_test auf const char* spezialisieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2471601</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2471601</guid><dc:creator><![CDATA[Techel]]></dc:creator><pubDate>Mon, 19 Oct 2015 11:38:06 GMT</pubDate></item><item><title><![CDATA[Reply to Initialisierung von char* via template gibt Warnung [gelöst] on Mon, 19 Oct 2015 12:03:21 GMT]]></title><description><![CDATA[<p>Ok, habe ich verstanden. Danke für die Antwort.</p>
<p>Da ich für den char* Typ noch einen weiteren Parameter im Konstruktor übergeben möchte, konnte ich keine Template Spezialisierung machen. Deswegen das enable_if geschwurbel <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>Werde es aber entsprechend anpassen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2471606</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2471606</guid><dc:creator><![CDATA[zussel]]></dc:creator><pubDate>Mon, 19 Oct 2015 12:03:21 GMT</pubDate></item></channel></rss>