<?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[HAS_MEMBER macro]]></title><description><![CDATA[<p>Hi,<br />
gerade im Zusammenhang mit static_assert und Templates wäre ein HAS_MEMBER Template/Macro oft sehr sinvoll. Deswegen habe ich mir folgendes überlegt:</p>
<pre><code class="language-cpp">#define DETAIL_DEF_HAS_MEMBER(type, x)					\
  struct HasMember {						\
    static auto check(float) -&gt; decltype(&amp;type::x);		\
    static void check(int);					\
  };                        

#define HAS_MEMBER(type, mem) ([=]() -&gt; bool { DETAIL_DEF_HAS_MEMBER(type, mem); return !std::is_same&lt;decltype(HasMember::check(1.0f) ), void&gt;::value; })()
</code></pre>
<p>Es scheint auch zu funktionieren (ich kenne mich mit Macros nicht so gut aus), allerdings frage ich mich, ob das auch ohne Macros geht, oder ob es vlt. schon so etwas in der Art in Boost gibt (ich habe nichts gefunden). Kennt ihr eine solche Funktion in Boost oder wisst ihr, ob das auch ohne Macros geht?</p>
<p>EDIT: Habe es gerade noch mal getestet: Es funktioniert leider nur wenn die Memberfunktion existiert, sonst nicht :(. Hat jemand eine Idee, um das zu beheben? (Es kommt ein Compiler Error: Fehler: Y ist kein Element von <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f635.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--dizzy_face"
      title="X)"
      alt="😵"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/299877/has_member-macro</link><generator>RSS for Node</generator><lastBuildDate>Thu, 13 Aug 2026 01:47:35 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/299877.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 20 Feb 2012 11:29:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to HAS_MEMBER macro on Mon, 20 Feb 2012 11:54:19 GMT]]></title><description><![CDATA[<p>Hi,<br />
gerade im Zusammenhang mit static_assert und Templates wäre ein HAS_MEMBER Template/Macro oft sehr sinvoll. Deswegen habe ich mir folgendes überlegt:</p>
<pre><code class="language-cpp">#define DETAIL_DEF_HAS_MEMBER(type, x)					\
  struct HasMember {						\
    static auto check(float) -&gt; decltype(&amp;type::x);		\
    static void check(int);					\
  };                        

#define HAS_MEMBER(type, mem) ([=]() -&gt; bool { DETAIL_DEF_HAS_MEMBER(type, mem); return !std::is_same&lt;decltype(HasMember::check(1.0f) ), void&gt;::value; })()
</code></pre>
<p>Es scheint auch zu funktionieren (ich kenne mich mit Macros nicht so gut aus), allerdings frage ich mich, ob das auch ohne Macros geht, oder ob es vlt. schon so etwas in der Art in Boost gibt (ich habe nichts gefunden). Kennt ihr eine solche Funktion in Boost oder wisst ihr, ob das auch ohne Macros geht?</p>
<p>EDIT: Habe es gerade noch mal getestet: Es funktioniert leider nur wenn die Memberfunktion existiert, sonst nicht :(. Hat jemand eine Idee, um das zu beheben? (Es kommt ein Compiler Error: Fehler: Y ist kein Element von <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f635.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--dizzy_face"
      title="X)"
      alt="😵"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2183368</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2183368</guid><dc:creator><![CDATA[pyhax]]></dc:creator><pubDate>Mon, 20 Feb 2012 11:54:19 GMT</pubDate></item><item><title><![CDATA[Reply to HAS_MEMBER macro on Mon, 20 Feb 2012 13:16:59 GMT]]></title><description><![CDATA[<p>Das Beispiel hier funktioniert schonmal solange der Member nicht private ist:</p>
<pre><code class="language-cpp">struct MemberTest
{
    template&lt;typename T&gt; static auto test(T *) -&gt; decltype(&amp;T::x);
    static void test(...);
};

#define HAS_X(type) (!std::is_same&lt;decltype(MemberTest::test((type *) 0)), void&gt;::value)
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2183427</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2183427</guid><dc:creator><![CDATA[wxSkip]]></dc:creator><pubDate>Mon, 20 Feb 2012 13:16:59 GMT</pubDate></item><item><title><![CDATA[Reply to HAS_MEMBER macro on Mon, 20 Feb 2012 13:19:41 GMT]]></title><description><![CDATA[<p>Ja ich weiß, bloß ich möchte das ganze gerne ohne das man ein HAS_X, ein HAS_Y, ein HAS_Z, ein HAS_... schreiben muss.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2183430</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2183430</guid><dc:creator><![CDATA[pyhax]]></dc:creator><pubDate>Mon, 20 Feb 2012 13:19:41 GMT</pubDate></item><item><title><![CDATA[Reply to HAS_MEMBER macro on Mon, 20 Feb 2012 13:26:27 GMT]]></title><description><![CDATA[<p>pyhax schrieb:</p>
<blockquote>
<p>Ja ich weiß, bloß ich möchte das ganze gerne ohne das man ein HAS_X, ein HAS_Y, ein HAS_Z, ein HAS_... schreiben muss.</p>
</blockquote>
<p>Das hab ich bloß weggelassen, du kannst ja dein Define wieder reinmachen. (Oder liegt der Fehler gerade dort?)</p>
<p>EDIT: Moment, ich schau mal nach...<br />
EDIT2: Nein, wenn du meinen Code statt deinem noch reindefinierst, sollte es funktionieren. Das Problem ist, dass der Compiler durch dein #define ohne template-check-Funktion meint &quot;Das wird nie funktionieren&quot;, wohingegen er bei einer template-check-Funktion halt nur sieht &quot;Ok, bei diesem Typ klappts nicht, also nehmen wir die andere&quot;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2183431</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2183431</guid><dc:creator><![CDATA[wxSkip]]></dc:creator><pubDate>Mon, 20 Feb 2012 13:26:27 GMT</pubDate></item><item><title><![CDATA[Reply to HAS_MEMBER macro on Mon, 20 Feb 2012 15:30:03 GMT]]></title><description><![CDATA[<p>Leider geht das nicht:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

#define DEF_HAS_MEMBER(x)						\
  struct HasMember {							\
    template &lt;class T&gt;							\
    static auto check(float) -&gt; decltype(&amp;T::x);			\
    template &lt;class T&gt;							\
    static auto check(int) -&gt; void;					\
  };                        

#define HAS_MEMBER(type, mem) ([=]() -&gt; bool { DEF_HAS_MEMBER(mem); return !std::is_same&lt;decltype(HasMember::check&lt;type&gt;(1.0f) ), void&gt;::value; })()

class X {
  public:
    void Y() {}
};

int main() {
  std::cout &lt;&lt; HAS_MEMBER(X, Y) &lt;&lt; std::endl;
}
</code></pre>
<p>Fehler: ungültige Deklaration eines Elementtemplates in lokaler Klasse</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2183488</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2183488</guid><dc:creator><![CDATA[pyhax]]></dc:creator><pubDate>Mon, 20 Feb 2012 15:30:03 GMT</pubDate></item></channel></rss>