<?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[Casten von Enums]]></title><description><![CDATA[<p>Ist es bei folgendem Code nötig explicit zu casten? Zumindest gibt es keine Meldungen vom Compiler (MSVC-9/10) hinsichtlich Typumwandlung, wenn ich nicht explizit caste.</p>
<pre><code class="language-cpp">#ifndef COLOR_H_
#define COLOR_H_

#include &lt;assert.h&gt;  // assert()

// Constants

enum {
  COLOR_WHITE = 0,
  COLOR_BLACK = 1,
  COLOR_NONE = 2,
};

// Inline functions

inline int color_is_ok(int color) {
  return    color == static_cast&lt;int&gt;(COLOR_WHITE)
         || color == static_cast&lt;int&gt;(COLOR_BLACK);  // are these casts necessary?
}

inline int flip_color(int color) {
  assert(color_is_ok(color));
  return static_cast&lt;int&gt;(color) ^ 1;  // is this cast necessary?
}

#endif  // COLOR_H_
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/282233/casten-von-enums</link><generator>RSS for Node</generator><lastBuildDate>Sun, 23 Aug 2026 08:09:58 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/282233.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 16 Feb 2011 12:39:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Casten von Enums on Wed, 16 Feb 2011 12:42:47 GMT]]></title><description><![CDATA[<p>Ist es bei folgendem Code nötig explicit zu casten? Zumindest gibt es keine Meldungen vom Compiler (MSVC-9/10) hinsichtlich Typumwandlung, wenn ich nicht explizit caste.</p>
<pre><code class="language-cpp">#ifndef COLOR_H_
#define COLOR_H_

#include &lt;assert.h&gt;  // assert()

// Constants

enum {
  COLOR_WHITE = 0,
  COLOR_BLACK = 1,
  COLOR_NONE = 2,
};

// Inline functions

inline int color_is_ok(int color) {
  return    color == static_cast&lt;int&gt;(COLOR_WHITE)
         || color == static_cast&lt;int&gt;(COLOR_BLACK);  // are these casts necessary?
}

inline int flip_color(int color) {
  assert(color_is_ok(color));
  return static_cast&lt;int&gt;(color) ^ 1;  // is this cast necessary?
}

#endif  // COLOR_H_
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2021759</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021759</guid><dc:creator><![CDATA[Tomahawk]]></dc:creator><pubDate>Wed, 16 Feb 2011 12:42:47 GMT</pubDate></item><item><title><![CDATA[Reply to Casten von Enums on Wed, 16 Feb 2011 12:59:48 GMT]]></title><description><![CDATA[<p>Enum-Werte können implizit in int umgewandelt werden, die Casts sind also unnötig.<br />
Der umgekehrte Weg hingegen bedarf einer expliziten Typumwandlung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021772</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021772</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 16 Feb 2011 12:59:48 GMT</pubDate></item><item><title><![CDATA[Reply to Casten von Enums on Wed, 16 Feb 2011 13:02:46 GMT]]></title><description><![CDATA[<p>enums unterliegen der <em>integral promotion</em>, die Casts sind daher unnötig. Das heißt ein enum-element ist ok, wo ein int erwartet wird, aber ein int ist nicht ok, wenn ein Element des enums erwartet wird.</p>
<p>edit: zu langsam...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021773</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021773</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 16 Feb 2011 13:02:46 GMT</pubDate></item><item><title><![CDATA[Reply to Casten von Enums on Wed, 16 Feb 2011 13:06:15 GMT]]></title><description><![CDATA[<blockquote>
<p>There is an implicit conversion from any enum type to int. But not vice versa.</p>
</blockquote>
<p>Aber wenn du schon ein enum erstellst, wieso verwendest du diesen Typ dann nicht einfach als Parameter oder Rückgabewert?</p>
<p>Das abgeänderte Beispiel soll nur dazu dienen um dir zu zeigen was ich damit meine und sollte so vielleicht besser nicht übernommen werden.</p>
<pre><code class="language-cpp">#ifndef COLOR_H_
#define COLOR_H_

#include &lt;assert.h&gt;

enum colors 
{
  COLOR_NONE,
  COLOR_WHITE,
  COLOR_BLACK
};

inline bool color_is_ok(colors color) const
{
  return color==COLOR_WHITE || color==COLOR_BLACK
}

inline colors flip_color(colors color) const
{
  assert(color_is_ok(color));
  if(color_is_ok(color))
     return (color==COLOR_BLACK)?COLOR_WHITE:COLOR_BLACK
  else
     return COLOR_NONE;
}

#endif  // COLOR_H_
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2021777</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021777</guid><dc:creator><![CDATA[enumerat0r]]></dc:creator><pubDate>Wed, 16 Feb 2011 13:06:15 GMT</pubDate></item><item><title><![CDATA[Reply to Casten von Enums on Wed, 16 Feb 2011 17:28:19 GMT]]></title><description><![CDATA[<p>Warum gibt es immer Leute, die den Enumeratoren Werte zuweisen, die sie ohnehin haben? Zumal die konkreten Zahlenwerte sowieso meistens nicht interessant sind...</p>
<p>Und ich würde in C++ den Header <code>&lt;cassert&gt;</code> verwenden. Ansonsten schliesse ich mich enumerat0r an, abgesehen von dem folgenden Code, wo das <code>if</code> neben dem <code>assert</code> unnötig ist. Und so wie ich Tomahawk kenne, hat er Angst davor, dass der Compiler <code>?:</code> nicht wegoptimieren kann, und nimmt deshalb das massiv schnellere <code>^</code> .</p>
<pre><code class="language-cpp">assert(color_is_ok(color)); 
  if(color_is_ok(color)) 
     return (color==COLOR_BLACK)?COLOR_WHITE:COLOR_BLACK 
  else 
     return COLOR_NONE;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2021940</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021940</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 16 Feb 2011 17:28:19 GMT</pubDate></item></channel></rss>