<?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[VS2013.4 variadic macros: std::array&amp;lt;std::string,.. füllen]]></title><description><![CDATA[<p>Hallo!</p>
<p>Wie man im Code sehen kann, hätte ich gerne in type[0] &quot;E1&quot; und in type[1] &quot;E2&quot; stehen.<br />
Ist das irgendwie möglich?</p>
<p>Leider steht so in type[0] &quot;E1, E2&quot; und type[1] ist leer:</p>
<pre><code>#define EXPAND(x) x
#define ARG_N(_1,_2,_3,_4,_5,_6,N,...) N
#define NARG_(...) EXPAND(ARG_N(__VA_ARGS__))
#define NARG(...) NARG_(__VA_ARGS__,6,5,4,3,2,1,0)

#define DefineClass(name, ...) \
class name \
{ \
public: \
	std::array&lt;std::string, NARG(__VA_ARGS__)&gt; type; \
public: \
	enum Type \
	{ \
		__VA_ARGS__ \
	}; \
	name() : type({#__VA_ARGS__}){ } \
};

DefineClass(Test1, E1, E2);

int main()
{
	Test1 t;

	cout &lt;&lt; t.type[0] &lt;&lt; endl; // &quot;E1, E2&quot;
	cout &lt;&lt; t.type[1] &lt;&lt; endl; // &quot;&quot; (leer)
}
</code></pre>
<p>Eine weitere Frage noch: Geht das mit dem NARG(__VA_ARGS__) als array-Größe noch einfacher?</p>
<p>Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/329967/vs2013-4-variadic-macros-std-array-lt-std-string-füllen</link><generator>RSS for Node</generator><lastBuildDate>Fri, 03 Jul 2026 13:59:19 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/329967.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 16 Dec 2014 16:57:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to VS2013.4 variadic macros: std::array&amp;lt;std::string,.. füllen on Tue, 16 Dec 2014 16:57:28 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Wie man im Code sehen kann, hätte ich gerne in type[0] &quot;E1&quot; und in type[1] &quot;E2&quot; stehen.<br />
Ist das irgendwie möglich?</p>
<p>Leider steht so in type[0] &quot;E1, E2&quot; und type[1] ist leer:</p>
<pre><code>#define EXPAND(x) x
#define ARG_N(_1,_2,_3,_4,_5,_6,N,...) N
#define NARG_(...) EXPAND(ARG_N(__VA_ARGS__))
#define NARG(...) NARG_(__VA_ARGS__,6,5,4,3,2,1,0)

#define DefineClass(name, ...) \
class name \
{ \
public: \
	std::array&lt;std::string, NARG(__VA_ARGS__)&gt; type; \
public: \
	enum Type \
	{ \
		__VA_ARGS__ \
	}; \
	name() : type({#__VA_ARGS__}){ } \
};

DefineClass(Test1, E1, E2);

int main()
{
	Test1 t;

	cout &lt;&lt; t.type[0] &lt;&lt; endl; // &quot;E1, E2&quot;
	cout &lt;&lt; t.type[1] &lt;&lt; endl; // &quot;&quot; (leer)
}
</code></pre>
<p>Eine weitere Frage noch: Geht das mit dem NARG(__VA_ARGS__) als array-Größe noch einfacher?</p>
<p>Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2433136</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2433136</guid><dc:creator><![CDATA[Hallo1]]></dc:creator><pubDate>Tue, 16 Dec 2014 16:57:28 GMT</pubDate></item><item><title><![CDATA[Reply to VS2013.4 variadic macros: std::array&amp;lt;std::string,.. füllen on Tue, 16 Dec 2014 18:10:03 GMT]]></title><description><![CDATA[<p>Ahh, sry, ich hätte das enum drin lassen sollen.<br />
Ich möchte nämlich sowohl ein enum als auch ein string-array erstellen.</p>
<p>Es kommt also noch Folgendes in die Klasse (das funktioniert):</p>
<pre><code>enum Type \
{ \
	__VA_ARGS__ \
}; \*/
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2433155</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2433155</guid><dc:creator><![CDATA[Hallo1]]></dc:creator><pubDate>Tue, 16 Dec 2014 18:10:03 GMT</pubDate></item><item><title><![CDATA[Reply to VS2013.4 variadic macros: std::array&amp;lt;std::string,.. füllen on Tue, 16 Dec 2014 18:51:19 GMT]]></title><description><![CDATA[<p>Hmm, ich mach das jetzt so:</p>
<pre><code>#define EXPAND(x) x
#define ARG_N(_1,_2,_3,_4,_5,_6,N,...) N
#define NARG(...) EXPAND(ARG_N(__VA_ARGS__,6,5,4,3,2,1,0))

#define DefineClass(name, ...) \
class name##_hlp \
{ \
public: \
	std::array&lt;std::wstring, NARG(__VA_ARGS__)&gt; type; \
	name##_hlp() : type({{L#__VA_ARGS__}}){ auto t = Tokenize(type[0], L','); int c = 0; for(auto x : t) type[c++] = TrimString(x); } \
}; \
name##_hlp _##name##_hlp; \
class name \
{ \
public: \
	enum Type \
	{ \
		__VA_ARGS__ \
	}; \
public: \
	name() { } \
	std::wstring GetStr(unsigned int type) { return _##name##_hlp.type[type];} \
};

DefineClass(Test1, E1, E2, E3);
DefineClass(Test2, E4, F5);

int main()
{
	Test1 t;
	Test2 t2;

	wcout &lt;&lt; t.GetStr(Test1::E2) &lt;&lt; endl; // &quot;E2&quot; juhuu
	wcout &lt;&lt; t2.GetStr(Test2::F5) &lt;&lt; endl; // &quot;F5&quot;
}
</code></pre>
<p>Ist zwar irgendwie etwas unschön, aber immerhin nur an einer Stelle <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /><br />
Außerdem wird das Array so nur einmalig beim Programmstart mit Tokenize/TrimString initialisiert, und nicht mit jeder Instanz von Test1 etc.</p>
<p>Weiß wer wie's besser geht? <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2433164</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2433164</guid><dc:creator><![CDATA[Hallo1]]></dc:creator><pubDate>Tue, 16 Dec 2014 18:51:19 GMT</pubDate></item><item><title><![CDATA[Reply to VS2013.4 variadic macros: std::array&amp;lt;std::string,.. füllen on Tue, 16 Dec 2014 19:51:36 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/u109509" rel="nofollow">SeppJ</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/f3" rel="nofollow">Compiler- und IDE-Forum</a> in das Forum <a href="http://www.c-plusplus.net/forum/f15" rel="nofollow">C++ (alle ISO-Standards)</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/39405" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2433177</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2433177</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Tue, 16 Dec 2014 19:51:36 GMT</pubDate></item></channel></rss>