<?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[&amp;quot;TypeTrait&amp;quot; static &#x2F; extern]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich breche mein Problem mal bis aufs nötigste herunter. Ich habe folgende (Beispiel-)Klassen:</p>
<pre><code class="language-cpp">template&lt;bool b&gt;
struct TypeTrait
{
	static int x;
};

template&lt;&gt;
int TypeTrait&lt;true&gt;::x;

template&lt;&gt;
struct TypeTrait&lt;false&gt;
{
	int x;
}

template&lt;bool b&gt;
class MyClass : private TypeTrait&lt;b&gt;
{
	/*static*/ void foo(int y)
	{
		x += y;
		cout &lt;&lt; x &lt;&lt; endl;
	}
};
</code></pre>
<p>void MyClass::foo(int) soll im Falle das &quot;b&quot; &quot;true&quot; ist, static sein.<br />
Ich suche nach einer eleganten Art dies zu lösen; ohne Spezialisierung und den identischen (!) Body neuschreiben zu müssen.</p>
<p>Hat da jemand eine elegantere Lösung?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/270442/quot-typetrait-quot-static-extern</link><generator>RSS for Node</generator><lastBuildDate>Sun, 30 Aug 2026 08:10:32 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/270442.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 12 Jul 2010 17:32:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to &amp;quot;TypeTrait&amp;quot; static &#x2F; extern on Mon, 12 Jul 2010 17:32:00 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich breche mein Problem mal bis aufs nötigste herunter. Ich habe folgende (Beispiel-)Klassen:</p>
<pre><code class="language-cpp">template&lt;bool b&gt;
struct TypeTrait
{
	static int x;
};

template&lt;&gt;
int TypeTrait&lt;true&gt;::x;

template&lt;&gt;
struct TypeTrait&lt;false&gt;
{
	int x;
}

template&lt;bool b&gt;
class MyClass : private TypeTrait&lt;b&gt;
{
	/*static*/ void foo(int y)
	{
		x += y;
		cout &lt;&lt; x &lt;&lt; endl;
	}
};
</code></pre>
<p>void MyClass::foo(int) soll im Falle das &quot;b&quot; &quot;true&quot; ist, static sein.<br />
Ich suche nach einer eleganten Art dies zu lösen; ohne Spezialisierung und den identischen (!) Body neuschreiben zu müssen.</p>
<p>Hat da jemand eine elegantere Lösung?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1925181</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1925181</guid><dc:creator><![CDATA[FrEEzE2046]]></dc:creator><pubDate>Mon, 12 Jul 2010 17:32:00 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;TypeTrait&amp;quot; static &#x2F; extern on Tue, 13 Jul 2010 07:17:55 GMT]]></title><description><![CDATA[<p>Ohne Spezialisierung wird es schwer. Das Neuschreiben kann man sich über eine zusätzliche Indirektion sparen:</p>
<pre><code class="language-cpp">void foo_impl(int &amp; x, int y)
{
    x += y;
    std::cout &lt;&lt; x &lt;&lt; '\n';
}

template&lt;bool b&gt;
struct TypeTrait //eigentlich eher ein Verhalten
{
    static void foo(int y)
    {
        foo_impl(x, y);
    }
    static int x;
};

template&lt;bool b&gt; int TypeTrait&lt;b&gt;::x;

template&lt;&gt;
struct TypeTrait&lt;false&gt;
{
    void foo(int y)
    {
        foo_impl(x, y);
    }
    int x;
};

template&lt;bool b&gt;
class MyClass : private TypeTrait&lt;b&gt;
{
public:
    using TypeTrait&lt;b&gt;::foo;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1925364</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1925364</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Tue, 13 Jul 2010 07:17:55 GMT</pubDate></item></channel></rss>