<?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 vs. #define!?]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich spiele grad bisschen mit C++ template rum, und hab mal ein &quot;blöde&quot; Frage:</p>
<p>sagen wir mal ich habe ein struct</p>
<pre><code>typedef struct Comp
{
	uint16_t ValA;
	uint16_t ValB;
	uint16_t ValC;
	uint16_t ValD;
} Comp;
</code></pre>
<p>und will mittels eine makros generisch auf einen best. parmater des structur selektieren:</p>
<pre><code>#define Selector(x,Y) (uint16_t)(((Comp)x).Val##Y);

uint16_t t=  Selector(a,A);
</code></pre>
<p>geht sowa auch via templates ?</p>
<pre><code>template &lt;???&gt;
class Selector
{
	public:
		Selector(Comp inst)
		{
			uint16_t x = inst.Val???;
		}
};
</code></pre>
<p>bitte fragt mich nich nach dem grund.. sonder einfach nur ob es möglich ist?</p>
<p>Grüßle</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/330990/template-vs-define</link><generator>RSS for Node</generator><lastBuildDate>Thu, 02 Jul 2026 16:32:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/330990.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 05 Feb 2015 13:31:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to template vs. #define!? on Thu, 05 Feb 2015 13:31:10 GMT]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich spiele grad bisschen mit C++ template rum, und hab mal ein &quot;blöde&quot; Frage:</p>
<p>sagen wir mal ich habe ein struct</p>
<pre><code>typedef struct Comp
{
	uint16_t ValA;
	uint16_t ValB;
	uint16_t ValC;
	uint16_t ValD;
} Comp;
</code></pre>
<p>und will mittels eine makros generisch auf einen best. parmater des structur selektieren:</p>
<pre><code>#define Selector(x,Y) (uint16_t)(((Comp)x).Val##Y);

uint16_t t=  Selector(a,A);
</code></pre>
<p>geht sowa auch via templates ?</p>
<pre><code>template &lt;???&gt;
class Selector
{
	public:
		Selector(Comp inst)
		{
			uint16_t x = inst.Val???;
		}
};
</code></pre>
<p>bitte fragt mich nich nach dem grund.. sonder einfach nur ob es möglich ist?</p>
<p>Grüßle</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441331</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441331</guid><dc:creator><![CDATA[NullBockException]]></dc:creator><pubDate>Thu, 05 Feb 2015 13:31:10 GMT</pubDate></item><item><title><![CDATA[Reply to template vs. #define!? on Thu, 05 Feb 2015 13:47:06 GMT]]></title><description><![CDATA[<p>std::tuple... <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2441344</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441344</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Thu, 05 Feb 2015 13:47:06 GMT</pubDate></item><item><title><![CDATA[Reply to template vs. #define!? on Thu, 05 Feb 2015 13:48:26 GMT]]></title><description><![CDATA[<p>Soweit ich weiß, geht das im allgemeinen nicht. Das ginge höchstens mit Reflections und ob und wann die mal Teil des Standards werden steht in den Sternen. Man könnte vielleicht ein Template für jeden sinnvollen char wert spezialisieren, aber da muss ich noch mal drüber nachdenken bevor ich ein Beispiel präsentieren kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441345</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441345</guid><dc:creator><![CDATA[TNA]]></dc:creator><pubDate>Thu, 05 Feb 2015 13:48:26 GMT</pubDate></item><item><title><![CDATA[Reply to template vs. #define!? on Thu, 05 Feb 2015 13:59:18 GMT]]></title><description><![CDATA[<p>Vielleicht so etwas in der Art (ungetestet):</p>
<pre><code>template&lt;class T&gt;
class selector&lt;char&gt; {
};

template&lt;class T&gt;
class selector&lt;'A'&gt; {
static auto select(T obj) -&gt; decltype( obj.ValA) {return obj.ValA;}
};

template&lt;class T&gt;
class selector&lt;'B'&gt; {
static auto select(T obj) -&gt; decltype( obj.ValB) {return obj.ValB;}
};
</code></pre>
<p>usw.</p>
<p>Dann könntest du So damit zugreifen:</p>
<pre><code>uint16_t t =  selector&lt;'A'&gt;::select(a);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2441347</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441347</guid><dc:creator><![CDATA[TNA]]></dc:creator><pubDate>Thu, 05 Feb 2015 13:59:18 GMT</pubDate></item><item><title><![CDATA[Reply to template vs. #define!? on Thu, 05 Feb 2015 16:03:17 GMT]]></title><description><![CDATA[<p>NullBockException schrieb:</p>
<blockquote>
<p>sagen wir mal ich habe ein struct</p>
<pre><code>typedef struct Comp
{
	uint16_t ValA;
	uint16_t ValB;
	uint16_t ValC;
	uint16_t ValD;
} Comp;
</code></pre>
</blockquote>
<p>Diese struct ist total unhandlich. Richtig modelliert sähe das so aus:</p>
<pre><code class="language-cpp">struct Comp {
        uint64_t Vals[4];
};
</code></pre>
<p>Dann kannst du dynamisch per Index zugreifen. Wenn du den Elementen Namen geben willst</p>
<pre><code class="language-cpp">enum class CompVal { A=0, B, C, D };
</code></pre>
<p>Alles andere (siehe Code von TNA) sind Workarounds um ein schlechtes zugrundeliegendes Design.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441383</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441383</guid><dc:creator><![CDATA[back2basics]]></dc:creator><pubDate>Thu, 05 Feb 2015 16:03:17 GMT</pubDate></item><item><title><![CDATA[Reply to template vs. #define!? on Thu, 05 Feb 2015 18:30:38 GMT]]></title><description><![CDATA[<p>Absolut... aber leider sind die structs teil einer api... hardware schnittstelle</p>
<p>ich mach es jetzt so ungefähr:</p>
<pre><code>uint16_t *p = ((uint16_t*)&amp;Comp);

p[0] = ...; // entspricht A
p[1] = ...; // entspricht B
</code></pre>
<p>usw;)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441427</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441427</guid><dc:creator><![CDATA[NullBockException]]></dc:creator><pubDate>Thu, 05 Feb 2015 18:30:38 GMT</pubDate></item><item><title><![CDATA[Reply to template vs. #define!? on Fri, 06 Feb 2015 02:54:59 GMT]]></title><description><![CDATA[<p>Naja es gibt Member-Pointer (&quot;pointer to member&quot;).<br />
Die C++ Variante von &quot;offsetof&quot; halt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441482</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441482</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Fri, 06 Feb 2015 02:54:59 GMT</pubDate></item><item><title><![CDATA[Reply to template vs. #define!? on Fri, 06 Feb 2015 05:06:34 GMT]]></title><description><![CDATA[<p>Guten Morgen Hustbear.. kannst du mir das mal genau erläutern:)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441484</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441484</guid><dc:creator><![CDATA[NullBockException]]></dc:creator><pubDate>Fri, 06 Feb 2015 05:06:34 GMT</pubDate></item><item><title><![CDATA[Reply to template vs. #define!? on Fri, 06 Feb 2015 05:43:19 GMT]]></title><description><![CDATA[<p>Ein Member-Pointer identifiziert sozusagen die &quot;Position&quot; eines Unterobjekts, also eines Member eines anderen Objekts. Ohne dabei aber das Hauptobjekt festzulegen.<br />
Die Syntax ist etwas gewöhnungsbedürftig:</p>
<pre><code class="language-cpp">typedef uint16_t Comp::*CompPart; // Typedef auf einen &quot;pointer to member&quot; (Deklaration ist wie üblich das selbe ohne das Keyword &quot;typedef&quot;)
</code></pre>
<p>Deine &quot;A&quot;, &quot;B&quot;, &quot;C&quot; werden dann einfach zu Instanzen dieses Typs.</p>
<pre><code class="language-cpp">typedef uint16_t Comp::*CompPart;

static CompPart const Comp_ValA = &amp;Comp::ValA;
static CompPart const Comp_ValB = &amp;Comp::ValB;
static CompPart const Comp_ValC = &amp;Comp::ValC;

uint16_t Selector(Comp const&amp; c, CompPart p)
{
    return c.*p; // c.*p ergibt das Member an &quot;Position&quot; p im Objekt c
}

Comp theComp;
uint16_t someA = Selector(theComp, Comp_ValA);
// oder direkt
uint16_t someB = Selector(theComp, &amp;Comp::ValB);
</code></pre>
<p>Geht natürlich genau so über einen non-type Template Parameter. (Wobei man bei Verwendung eines Tempaltes keine Member-Pointer bräuchte, man könnte dazu auch eine kleine Hilfsklasse pro Member schreiben die man dann als Type-Parameter an das Template übergibt.)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441489</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441489</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Fri, 06 Feb 2015 05:43:19 GMT</pubDate></item></channel></rss>