<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Basic C++ question: enum</title>
		<link>http://www.allegro.cc/forums/view/604470</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 02 Jul 2010 13:44:14 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The enum keyword seems like it would neat and convenient tool for me, except that it doesn&#39;t behave the way I&#39;d expect or want...</p><p>From <a href="http://enel.ucalgary.ca/People/Norman/enel315_winter1997/enum_types/">this</a> reference (chosen arbitrarily).</p><div class="quote_container"><div class="title"><a href="http://enel.ucalgary.ca/People/Norman/enel315_winter1997/enum_types/">the site</a> said:</div><div class="quote"><p>There is an implicit conversion from any enum type to int.<br />...<br />On the other hand, there is not an implicit conversion from int to an enum type</p></div></div><p>To me, this seems backwards. Suppose I had something like this:</p><p><span class="source-code"><span class="k1">enum</span> Month <span class="k2">{</span>jan<span class="k3">=</span><span class="n">1</span>, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec<span class="k2">}</span><span class="k2">;</span></span></p><p>It seems to me that it would be fairly natural to want to do something like this:<br /><span class="source-code">Month this_month <span class="k3">=</span> <span class="n">4</span><span class="k2">;</span></span></p><p>It&#39;s not a big deal that that isn&#39;t allowed. What is a big deal is this:<br /><span class="source-code"><span class="k1">int</span> an_unrelated_number <span class="k3">=</span> mar<span class="k2">;</span> <span class="c">//lolwut?</span></span><br />I just don&#39;t understand why this is allowed! I&#39;d like to use enum, but I don&#39;t what my enums to clutter up the namespace in this way.</p><p>So my question is this: is there some trick I can use to define an enum, like the Month thing above, without implicitly defining a bunch of constant ints that I won&#39;t ever need outside of the Month enum.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Karadoc ~~)</author>
		<pubDate>Mon, 28 Jun 2010 13:22:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>enum can be converted to int, int cannot be converted to enum at least because two different enum values can hold the same integer value.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (type568)</author>
		<pubDate>Mon, 28 Jun 2010 14:42:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Tobias answered this question for me once <a href="http://www.allegro.cc/forums/thread/604238/868492#target">here</a>, and then again a few days ago <a href="http://www.allegro.cc/forums/thread/604447/872300#target">here</a>.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Mon, 28 Jun 2010 15:09:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/872716#target">Karadoc ~~</a> said:</div><div class="quote"><p>
I just don&#39;t understand why this is allowed! I&#39;d like to use enum, but I don&#39;t what my enums to clutter up the namespace in this way.
</p></div></div><p>

You can always define enums within a class/struct/namespace:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">struct</span> DAY <span class="k2">{</span>
<span class="number">  2</span>    <span class="k1">enum</span> VALUE <span class="k2">{</span>
<span class="number">  3</span>        MON,
<span class="number">  4</span>        TUE,
<span class="number">  5</span>        WED,
<span class="number">  6</span>        THU,
<span class="number">  7</span>        FRI,
<span class="number">  8</span>        SAT,
<span class="number">  9</span>        SUN
<span class="number"> 10</span>    <span class="k2">}</span><span class="k2">;</span>
<span class="number"> 11</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 12</span>
<span class="number"> 13</span><span class="c">//or:</span>
<span class="number"> 14</span><span class="k1">namespace</span> DAY <span class="k2">{</span>
<span class="number"> 15</span>    <span class="k1">enum</span> VALUE <span class="k2">{</span>
<span class="number"> 16</span>        MON,
<span class="number"> 17</span>        TUE,
<span class="number"> 18</span>        WED,
<span class="number"> 19</span>        THU,
<span class="number"> 20</span>        FRI,
<span class="number"> 21</span>        SAT,
<span class="number"> 22</span>        SUN
<span class="number"> 23</span>    <span class="k2">}</span><span class="k2">;</span>
<span class="number"> 24</span><span class="k2">}</span>
</div></div><p>

And then you can happily use it like this:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> day1 <span class="k3">=</span> DAY::MON<span class="k2">;</span>
Day::VALUE day2 <span class="k3">=</span> DAY::WED<span class="k2">;</span>
</pre></div></div><p>

You can also make completely typesafe enums, by defining a class that holds the values and a template class that contains the enumerated value:</p><div class="source-code snippet"><div class="inner"><pre><span class="c">//enum template</span>
<span class="k1">template</span> <span class="k3">&lt;</span><span class="k1">class</span> T&gt; <span class="k1">class</span> Enum <span class="k2">:</span> <span class="k1">public</span> T <span class="k2">{</span>
public:
    <span class="c">//bla bla constructors, destructor, setters, getters, etc.</span>

private:
    <span class="k1">typename</span> T::VALUE m_value<span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>

<span class="c">//only if DAY is not a namespace</span>
<span class="k1">typedef</span> Enum<span class="k3">&lt;</span>DAY&gt; Day<span class="k2">;</span>
</pre></div></div><p>

And then you have a completely typesafe enum. For example, this is not possible:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> d <span class="k3">=</span> Day::MON<span class="k2">;</span>
</pre></div></div><p>

while this is:</p><div class="source-code snippet"><div class="inner"><pre>Day d <span class="k3">=</span> Day::MON<span class="k2">;</span>
</pre></div></div><p>

Personally, I prefer the class solution over the namespace solution because I always need to encapsulate retrospection information in my enums, so as that the GUI is automatically created from the enums ;-).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Mon, 28 Jun 2010 15:37:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The reason for not allowing an implicit int -&gt; enum conversion seems ... reasonable; and axilmar&#39;s examples look like they do just what I was asking for. So thanks for all that.</p><p>Also, I did a little bit of testing with regard to the enum namespace thing...
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">enum</span> Day <span class="k2">{</span>mon <span class="k3">=</span> <span class="n">0</span>, tue, wed, thr, fri<span class="k2">}</span><span class="k2">;</span>
<span class="c">//...</span>
<span class="k2">{</span>
  <span class="k1">int</span> test <span class="k3">=</span> tue<span class="k2">;</span> <span class="c">// this is allowed, and now test == 1</span>
  Day today <span class="k3">=</span> tue<span class="k2">;</span>
<span class="k2">}</span>
<span class="k2">{</span>
  <span class="k1">int</span> test <span class="k3">=</span> tue<span class="k2">;</span>
  <span class="k1">int</span> tue <span class="k3">=</span> <span class="n">5</span><span class="k2">;</span>
  <span class="k1">int</span> test2 <span class="k3">=</span> tue<span class="k2">;</span>
  <span class="c">// now test == 1 and test2 == 5; Everything is fine so far</span>
  Day today <span class="k3">=</span> tue<span class="k2">;</span> <span class="c">// but this is not allowed</span>
<span class="k2">}</span>
</pre></div></div><p>
So the result of all that is that although the enum stuff does hang around even when it isn&#39;t wanted, it does a decent job of staying out of the way.</p><p>For what I&#39;m doing, I think I&#39;ll try axilmar&#39;s class thing. (edit - ** see below)</p><p>--</p><p>type568, I don&#39;t think what you said about multiple enum values being the same int value is correct – because of this:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">enum</span> Day <span class="k2">{</span>mon <span class="k3">=</span> <span class="n">0</span>, tue <span class="k3">=</span> <span class="n">0</span>, wed, thr, fri<span class="k2">}</span><span class="k2">;</span>

Day today <span class="k3">=</span> mon<span class="k2">;</span>

<span class="k1">if</span> <span class="k2">(</span>today <span class="k3">=</span><span class="k3">=</span> tue<span class="k2">)</span>
    cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"tuesday!"</span><span class="k2">;</span>
</pre></div></div><p>
This code results in &quot;tuesday!&quot; being printed. The point is that although multiple labels can apply to one number, it is still just one number. So this doesn&#39;t create a problem for converting ints to enums.</p><p>--<br />[edit]<br />I&#39;m having some trouble getting that class solution to work. I guess I didn&#39;t really understand what I was meant to do. Here&#39;s what I&#39;ve got so far:
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">struct</span> DAY
<span class="number">  2</span><span class="k2">{</span>
<span class="number">  3</span>    <span class="k1">enum</span> VALUE <span class="k2">{</span> mon, tue, wed, thu, fri<span class="k2">}</span><span class="k2">;</span>
<span class="number">  4</span><span class="k2">}</span><span class="k2">;</span>
<span class="number">  5</span>
<span class="number">  6</span><span class="k1">template</span> <span class="k3">&lt;</span><span class="k1">typename</span> T&gt; <span class="k1">class</span> Enum <span class="k2">:</span> <span class="k1">public</span> T
<span class="number">  7</span><span class="k2">{</span>
<span class="number">  8</span>    public:
<span class="number">  9</span>        <span class="k1">operator</span> <span class="k1">typename</span> T::VALUE<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span> <span class="k1">return</span> m_value<span class="k2">;</span> <span class="k2">}</span>
<span class="number"> 10</span>        <span class="k1">void</span> <span class="k1">operator</span><span class="k3">=</span><span class="k2">(</span><span class="k1">typename</span> T::VALUE v<span class="k2">)</span> <span class="k2">{</span> m_value <span class="k3">=</span> v<span class="k2">;</span> <span class="k2">}</span>
<span class="number"> 11</span>
<span class="number"> 12</span>    private:
<span class="number"> 13</span>        <span class="k1">typename</span> T::VALUE m_value<span class="k2">;</span>
<span class="number"> 14</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 15</span><span class="k1">typedef</span> Enum<span class="k3">&lt;</span>DAY&gt; Day<span class="k2">;</span>
<span class="number"> 16</span>
<span class="number"> 17</span><span class="c">// ...</span>
<span class="number"> 18</span>Day today<span class="k2">;</span>
<span class="number"> 19</span><span class="k1">int</span> x<span class="k2">;</span>
<span class="number"> 20</span>today <span class="k3">=</span> Day::mon<span class="k2">;</span> <span class="c">// allowed, which is good</span>
<span class="number"> 21</span>x <span class="k3">=</span> Day::mon<span class="k2">;</span> <span class="c">// also allowed, which is bad.</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Karadoc ~~)</author>
		<pubDate>Mon, 28 Jun 2010 18:49:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/872765#target">Karadoc ~~</a> said:</div><div class="quote"><p>
I&#39;m having some trouble getting that class solution to work.
</p></div></div><p>

You should not provide this operator:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">operator</span> <span class="k1">typename</span> T::VALUE<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span> <span class="k1">return</span> m_value<span class="k2">;</span> <span class="k2">}</span>
</pre></div></div><p>

Because C++ sees it and converts your class to int.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Mon, 28 Jun 2010 20:11:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok, but even with that line commented out, <span class="source-code">x <span class="k3">=</span> Day::mon<span class="k2">;</span></span> is compiled without warnings or errors. Actually I don&#39;t understand what would prevent it from being allowed. This &#39;Day&#39; class, made using the template, has all the properties that struct DAY has and they are all public. So if DAY::mon works, then Day::mon will also work.</p><p>I&#39;m trying to achieve what you described here:</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/872751#target">axilmar</a> said:</div><div class="quote"><p>And then you have a completely typesafe enum. For example, this is not possible:</p><p><span class="source-code"><span class="k1">int</span> d <span class="k3">=</span> Day::MON<span class="k2">;</span></span><br />while this is:</p><p><span class="source-code">Day d <span class="k3">=</span> Day::MON<span class="k2">;</span></span></p></div></div><p>If you&#39;ve done this in your own projects, would you mind posting a complete example? Because although I thought I understood how it worked before, I don&#39;t see any way forward now.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Karadoc ~~)</author>
		<pubDate>Tue, 29 Jun 2010 05:17:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/872716#target">Karadoc ~~</a> said:</div><div class="quote"><p>From this [enel.ucalgary.ca] reference (chosen arbitrarily).</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>There is an implicit conversion from any enum type to int.<br />...<br />On the other hand, there is not an implicit conversion from int to an enum type</p></div></div><p>
To me, this seems backwards. Suppose I had something like this:</p></div></div><p>
An enum is an integer (you can implicitly cast enum-&gt;int), but an integer isn&#39;t an enum (you can&#39;t implicitly cast int-&gt;enum). If you think about it, it makes sense. Lets say you had code like this:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">enum</span> Foo <span class="k2">{</span>
   VAL1 <span class="k3">=</span> <span class="n">1</span>,
   VAL2 <span class="k3">=</span> <span class="n">3</span>
<span class="k2">}</span><span class="k2">;</span>

<span class="k1">void</span> Func<span class="k2">(</span><span class="k1">int</span> ival<span class="k2">)</span>
<span class="k2">{</span>
    Foo eval <span class="k3">=</span> ival<span class="k2">;</span>
    ...
<span class="k2">}</span>
</pre></div></div><p>
Now, what should happen if you call <span class="source-code">Func<span class="k2">(</span><span class="n">0</span><span class="k2">)</span><span class="k2">;</span></span> or <span class="source-code">Func<span class="k2">(</span><span class="n">2</span><span class="k2">)</span><span class="k2">;</span></span>? If you try to forcefully cast it, you essentially break the type, since it has a value that&#39;s not part of its enumeration values.</p><p>You could argue it should know if given a determinable value, but that <i>would</i> cause an inconsistency as it could accept some integer variables and not others.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Tue, 29 Jun 2010 05:41:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Right, that seems fair. But I think an equally fair alternative would be to just treat enum as an int in which some particular values have names. So any int value is allowed by the enum, but not all values have special names. That&#39;s how it works in C, and I think it&#39;s fine*.</p><p>However, I completely understand the reasons behind not allowing implicit conversion from int to enum, and I think the reasons are sensible and fair. I just expected that it would be the same as in C, for consistency, but it isn&#39;t.</p><p>The main thing I don&#39;t like is the other side of things - the fact that the labels can be used outside of the enum (C has the same problem). I want the enum labels to apply <u>only</u> to the enum. I don&#39;t see any reason to spew the enum definitions across the entire namespace, and it prevents me from doing stuff like this:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">enum</span> Type1 <span class="k2">{</span>empty <span class="k3">=</span> <span class="n">0</span>, rock, sand, water, grass<span class="k2">}</span><span class="k2">;</span>
<span class="k1">enum</span> Type2 <span class="k2">{</span>empty <span class="k3">=</span> <span class="n">0</span>, road, water, tree, dirt<span class="k2">}</span><span class="k2">;</span>
<span class="c">// error: conflicting declaration 'water'</span>
<span class="c">// but all I want is for 'water' to refer to 3 when I'm using Type1, and 2 when using Type2. There should be no conflict. </span>
</pre></div></div><p>

If I wanted to define a bunch of constant ints, I&#39;d define a bunch of constant ints... like this <span class="source-code"><span class="k1">const</span> <span class="k1">int</span> empty <span class="k3">=</span> <span class="n">0</span>, rock <span class="k3">=</span> <span class="n">1</span>, sand <span class="k3">=</span> <span class="n">2</span>, water <span class="k3">=</span> <span class="n">3</span>, grass <span class="k3">=</span> <span class="n">4</span><span class="k2">;</span> <span class="c">// not what I want</span></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Karadoc ~~)</author>
		<pubDate>Tue, 29 Jun 2010 06:27:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/872873#target">Karadoc ~~</a> said:</div><div class="quote"><p>But I think an equally fair alternative would be to just treat enum as an int in which some particular values have names. So any int value is allowed by the enum, but not all values have special names. That&#39;s how it works in C, and I think it&#39;s fine*.</p></div></div><p>
I like to think of enums as strong integer types, such that an enum variable will only contain a valid enumeration value for it. With GCC, for example, if you do this:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">enum</span> Foo <span class="k2">{</span>
   VAL1, VAL2, VAL3
<span class="k2">}</span><span class="k2">;</span>

<span class="k1">void</span> Func<span class="k2">(</span>Foo val<span class="k2">)</span>
<span class="k2">{</span>
    <span class="k1">switch</span><span class="k2">(</span>val<span class="k2">)</span>
    <span class="k2">{</span>
    <span class="k1">case</span> VAL1:
        ...
    <span class="k1">case</span> VAL2:
        ...
    <span class="k2">}</span>
<span class="k2">}</span>
</pre></div></div><p>
It will throw a warning that VAL3 is not handled. And given that an enum can&#39;t contain an invalid enumeration value (unless you fail to initialize it, or forcefully cast a value), it&#39;s very useful for keeping a variable restrained to specific values. C++&#39;s stronger type safety just helps enforce that.</p><p>EDIT:
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><div class="source-code snippet"><div class="inner"><pre><span class="k1">enum</span> Type1 <span class="k2">{</span>empty <span class="k3">=</span> <span class="n">0</span>, rock, sand, water, grass<span class="k2">}</span><span class="k2">;</span>
<span class="k1">enum</span> Type2 <span class="k2">{</span>empty <span class="k3">=</span> <span class="n">0</span>, road, water, tree, dirt<span class="k2">}</span><span class="k2">;</span>
<span class="c">// error: conflicting declaration 'water'</span>
<span class="c">// but all I want is for 'water' to refer to 3 when I'm using Type1, and 2 when using Type2. There should be no conflict.</span>
</pre></div></div></div></div><p>
You can use a namespace:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">namespace</span> nType1 <span class="k2">{</span>
    <span class="k1">enum</span> Type1...<span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>
<span class="k1">using</span> <span class="k1">namespace</span> nType1<span class="k2">;</span>

<span class="k1">namespace</span> nType2 <span class="k2">{</span>
    <span class="k1">enum</span> Type2...<span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>
<span class="k1">using</span> <span class="k1">namespace</span> nType2<span class="k2">;</span>
</pre></div></div><p>
Though you&#39;ll have to use nType1::water or nType2::water, if there&#39;s a duplicate enum value name. You can also use operator overrides to handle casts from a Type1 enum value to a Type2 one, and vice-versa.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Tue, 29 Jun 2010 06:54:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/872876#target">Kitty Cat</a> said:</div><div class="quote"><p>
And given that an enum <b>can&#39;t</b> contain an invalid enumeration value (<b>unless</b> you fail to initialize it, or forcefully cast a value), it&#39;s very useful for keeping a variable restrained to specific values.
</p></div></div><p>
In C and C++, <span class="source-code"><span class="k1">enum</span></span> is just loosely controlled syntactic sugar over an <span class="source-code"><span class="k1">int</span></span>. The type doesn&#39;t mean anything in particular, except that certain values can be accessed by a meaningful name (albeit, the namespace issue pretty much forces the name to be uber long or potentially conflicting). <span class="source-code"><span class="k1">enum</span></span> types are not very type safe at all. You certainly can&#39;t rely on them. They basically provide the programmer with a semantically sensible alternative to magic numbers. Essentially, identical to a set of <span class="source-code"><span class="k1">const</span> <span class="k1">int</span></span>s and an <span class="source-code"><span class="k1">int</span></span>; except that C++ requires a cast. Requiring a cast is a good thing, but it still isn&#39;t something that you can rely on nobody doing. For that it would need to be impossible to cast to an <span class="source-code"><span class="k1">enum</span></span> type, which would have its own downfalls. Validation is your friend. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Tue, 29 Jun 2010 09:32:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve that feeling, that the more useless a feature is the more it is discussed.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (type568)</author>
		<pubDate>Tue, 29 Jun 2010 10:46:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/872911#target">type568</a> said:</div><div class="quote"><p> I&#39;ve that feeling, that the more useless a feature is the more it is discussed.</p></div></div><p>Of course!  Nobody argues about whether stuff that&#39;s obviously necessary should exist.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Tue, 29 Jun 2010 10:48:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In this case, I&#39;m hoping that our discussions will reveal a way of making enum less useless (more useful). Maybe people discuss useless stuff in general because that&#39;s the stuff that needs improving.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Karadoc ~~)</author>
		<pubDate>Tue, 29 Jun 2010 11:00:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That&#39;s the stuff that needs removing.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (type568)</author>
		<pubDate>Tue, 29 Jun 2010 11:04:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/872899#target">bamccaig</a> said:</div><div class="quote"><p>Requiring a cast is a good thing, but it still isn&#39;t something that you can rely on nobody doing. For that it would need to be impossible to cast to an enum type, which would have its own downfalls.</p></div></div><p>
You can say that about anything, really. If you can&#39;t rely on the code being safe with enum values (ie, not casting an unverified int value), then by the same token you couldn&#39;t rely on the code being safe with object pointers types (ie, not casting from a potentially incompatible pointer type to another; particularly from void*). Both require you to go out of your way to break it, in C++.</p><p>While it is true that pre-existing code may be more likely to break C++ enums than pointers, because people misunderstand their purpose and think they&#39;re just named integers or const int variables, it&#39;s a different case when you are in control of the code base. If you control the code, it&#39;s not difficult to make sure you don&#39;t cast unverified values (or preferrably not cast at all) from integers. When handled properly, enums can make the code more elegant and less error-prone.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Tue, 29 Jun 2010 12:05:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/872864#target">Karadoc ~~</a> said:</div><div class="quote"><p>
If you&#39;ve done this in your own projects, would you mind posting a complete example? Because although I thought I understood how it worked before, I don&#39;t see any way forward now.
</p></div></div><p>

Here is the code:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">class</span> DAY <span class="k2">{</span>
<span class="number">  2</span>public:
<span class="number">  3</span>    <span class="k1">enum</span> VALUE <span class="k2">{</span>
<span class="number">  4</span>        MON,
<span class="number">  5</span>        TUE,
<span class="number">  6</span>        WED,
<span class="number">  7</span>        THU,
<span class="number">  8</span>        FRI,
<span class="number">  9</span>        SAT,
<span class="number"> 10</span>        SUN
<span class="number"> 11</span>    <span class="k2">}</span><span class="k2">;</span>
<span class="number"> 12</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 13</span>
<span class="number"> 14</span>
<span class="number"> 15</span><span class="k1">template</span> <span class="k3">&lt;</span><span class="k1">class</span> T&gt; <span class="k1">class</span> Enum <span class="k2">:</span> <span class="k1">public</span> T <span class="k2">{</span>
<span class="number"> 16</span>public:
<span class="number"> 17</span>    Enum<span class="k2">(</span><span class="k1">typename</span> T::VALUE value <span class="k3">=</span> T::VALUE<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span> <span class="k2">:</span> m_value<span class="k2">(</span>value<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 18</span>    <span class="k2">}</span>
<span class="number"> 19</span>    
<span class="number"> 20</span>    Enum<span class="k3">&lt;</span>T&gt; <span class="k3">&amp;</span><span class="k1">operator</span> <span class="k3">=</span> <span class="k2">(</span><span class="k1">typename</span> T::VALUE value<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 21</span>        m_value <span class="k3">=</span> value<span class="k2">;</span>
<span class="number"> 22</span>        <span class="k1">return</span> <span class="k3">*</span><span class="k1">this</span><span class="k2">;</span>
<span class="number"> 23</span>    <span class="k2">}</span>
<span class="number"> 24</span>
<span class="number"> 25</span>private:
<span class="number"> 26</span>    <span class="k1">typename</span> T::VALUE m_value<span class="k2">;</span>
<span class="number"> 27</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 28</span>
<span class="number"> 29</span>
<span class="number"> 30</span><span class="k1">typedef</span> Enum<span class="k3">&lt;</span>DAY&gt; Day<span class="k2">;</span>
<span class="number"> 31</span>
<span class="number"> 32</span>
<span class="number"> 33</span><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 34</span>    Day day <span class="k3">=</span> Day::MON<span class="k2">;</span>
<span class="number"> 35</span>    
<span class="number"> 36</span>    <span class="k1">int</span> i<span class="k2">;</span>
<span class="number"> 37</span>    Day d<span class="k2">;</span>
<span class="number"> 38</span>    
<span class="number"> 39</span>    i <span class="k3">=</span> day<span class="k2">;</span>
<span class="number"> 40</span>    d <span class="k3">=</span> day<span class="k2">;</span>
<span class="number"> 41</span>    
<span class="number"> 42</span>    <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 43</span><span class="k2">}</span>
</div></div><p>

And here is the compiler error:</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
1&gt;e:\temp\test_enum\main.cpp(39) : error C2440: &#39;=&#39; : cannot convert from &#39;Day&#39; to &#39;int&#39;<br />1&gt;        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
</p></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Tue, 29 Jun 2010 16:56:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/872927#target">Kitty Cat</a> said:</div><div class="quote"><p>You can say that about anything, really. If you can&#39;t rely on the code being safe with enum values (ie, not casting an unverified int value), then by the same token you couldn&#39;t rely on the code being safe with object pointers types (ie, not casting from a potentially incompatible pointer type to another; particularly from void*).</p></div></div><p>
You can&#39;t. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> That&#39;s why modern languages, like Java and C#, make it more difficult or impossible to do such things. You can never really rely on them being correct. Unfortunately, most code is written by many people and it can be very difficult to spot such evils, even if you do review patches, etc.</p><p>The best thing to do is to validate when possible. You can&#39;t validate a pointer (aside from ensuring it isn&#39;t <span class="source-code"><span class="n">0</span></span>), but you can damn sure validate <span class="source-code"><span class="k1">enum</span></span> types. The easiest way is probably to handle them with a <span class="source-code"><span class="k1">switch</span></span> statement with a <span class="source-code"><span class="k1">default</span></span> case that handles the error or design the program so that an invalid <span class="source-code"><span class="k1">enum</span></span> value has no side effects (when appropriate). All I&#39;m saying is that you can&#39;t rely on them and that if you want your code to be closer to bullet proof then you should assume that the value can be invalid and handle that case.
</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/872960#target">axilmar</a> said:</div><div class="quote"><p><span class="source-code">T::VALUE<span class="k2">(</span><span class="k2">)</span></span></p></div></div><p>
Is that a function call? <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" /> WTF is going on there? <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Tue, 29 Jun 2010 19:25:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>ah. I understand now. What I had was on the right track but I thought that it would prevent <span class="source-code">i <span class="k3">=</span> Day::MON<span class="k2">;</span></span>, which it doesn&#39;t. But it does prevent <span class="source-code">i <span class="k3">=</span> day<span class="k2">;</span></span>.. So it isn&#39;t a complete solution to my complaint, but it is an improvement over the usual enum behavior. Thanks for that.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Is that a function call?  WTF is going on there? </p></div></div><p>That&#39;s meant to be the default initializer of <span class="source-code">T::VALUE</span>. So it should result in the default value of the enum. Actually, I the line needs to be <span class="source-code">Enum<span class="k2">(</span><span class="k1">typename</span> T::VALUE value <span class="k3">=</span> <span class="k1">typename</span> T::VALUE<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span> <span class="k2">:</span> m_value<span class="k2">(</span>value<span class="k2">)</span> <span class="k2">{</span><span class="k2">}</span></span>, I think. I don&#39;t really know why it needs the &quot;typename&quot; keyword, but the compiler was complaining without it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Karadoc ~~)</author>
		<pubDate>Tue, 29 Jun 2010 19:38:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/872989#target">Karadoc ~~</a> said:</div><div class="quote"><p>
but I thought that it would prevent i = Day::MON
</p></div></div><p>

Unfortunately, it&#39;s not possible.</p><p>In my never-to-be-released language, my enums where strongly typed sets of values and therefore converting from enum to integer or integer to enum wouldn&#39;t be possible ;-).</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
So it should result in the default value of the enum.
</p></div></div><p>

Actually, it just sets the value to 0, which may or may not be the default value. The correct approach would be to set the value to the enum&#39;s first member, but since enums are ints, it is just set to 0.</p><p>&lt;quote&gt;<br />the line needs to be Enum(typename T::VALUE value = typename T::VALUE()) : m_value(value) {}<br />&lt;/code&gt;</p><p>I thought so too, but the compiler complains if I put &#39;typename&#39; in front of &#39;T::VALUE()&#39;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Tue, 29 Jun 2010 20:04:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>C++0x <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Strongly_typed_enumerations">seems to solve all these problems</a>. If you declare an enum with <span class="source-code"><span class="k1">enum</span> <span class="k1">class</span></span> then it&#39;s strongly typed (no conversion from or to integers) and the scope of the values is inside the enum name.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Oscar Giner)</author>
		<pubDate>Tue, 29 Jun 2010 21:20:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That&#39;s good news about C++0x. I look forward to it. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /><br />In fact, maybe it&#39;s time I updated my gcc (which is currently v3.4.5). By the sounds of <a href="http://gcc.gnu.org/projects/cxx0x.html">this</a> I&#39;d be able to start using this feature right away. -- but then I&#39;d have to recompile allegro (wouldn&#39;t I?)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Karadoc ~~)</author>
		<pubDate>Wed, 30 Jun 2010 10:52:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/872986#target">bamccaig</a> said:</div><div class="quote"><p>
That&#39;s why modern languages, like Java and C#, make it more difficult or impossible to do such things. 
</p></div></div><p>
In C#, you can cast any enum back to its underlying integer type without a compiler error; the only thing that&#39;s required is that the cast be explicit. The enum behaves exactly as in C++.<br />Compile and run for your pleasure:
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">using</span> System<span class="k2">;</span>
<span class="number">  2</span>
<span class="number">  3</span><span class="k1">namespace</span> EnumCast
<span class="number">  4</span><span class="k2">{</span>
<span class="number">  5</span>  <span class="k1">class</span> Program
<span class="number">  6</span>  <span class="k2">{</span>
<span class="number">  7</span>    <span class="k1">enum</span> Shenanigans
<span class="number">  8</span>    <span class="k2">{</span>
<span class="number">  9</span>      Foo <span class="k3">=</span> <span class="n">0</span>,
<span class="number"> 10</span>      Bar <span class="k3">=</span> <span class="n">1</span>,
<span class="number"> 11</span>      Baz <span class="k3">=</span> <span class="n">3</span>
<span class="number"> 12</span>    <span class="k2">}</span>
<span class="number"> 13</span>
<span class="number"> 14</span>    <span class="k1">static</span> <span class="k1">void</span> Main<span class="k2">(</span>string<span class="k2">[</span><span class="k2">]</span> args<span class="k2">)</span>
<span class="number"> 15</span>    <span class="k2">{</span>
<span class="number"> 16</span>      Shenanigans s <span class="k3">=</span> Shenanigans.Foo<span class="k2">;</span>
<span class="number"> 17</span>      Console.WriteLine<span class="k2">(</span><span class="s">"Shenanigans is: {0}"</span>, s.ToString<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 18</span>      s <span class="k3">=</span> <span class="k2">(</span>Shenanigans<span class="k2">)</span><span class="n">2</span><span class="k2">;</span>
<span class="number"> 19</span>      Console.WriteLine<span class="k2">(</span><span class="s">"Shenanigans is now: {0}"</span>, s.ToString<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 20</span>      Console.ReadKey<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 21</span>    <span class="k2">}</span>
<span class="number"> 22</span>  <span class="k2">}</span>
<span class="number"> 23</span><span class="k2">}</span>
</div></div><p>

Seriously, the idea is not to make things 120% foolproof. The idea is to provide programmers with tools that prevent them from accidentally doing stupid things. If you want to assign a value to an enumeration variable that&#39;s not in the enumeration, you can, but the language makes you jump through a little inconvenient hoop, which should be enough to remind you that you&#39;re not supposed to do this unless you absolutely have to (e.g., when reading enum values from a data source that has no notion of enumerations, like a binary file, or when interfacing with a closed API that provides raw integers only).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Fri, 02 Jul 2010 01:24:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604470/873309#target">Tobias Dammers</a> said:</div><div class="quote"><p>
In C#, you can cast any enum back to its underlying integer type without a compiler error; the only thing that&#39;s required is that the cast be explicit. The enum behaves exactly as in C++.</p></div></div><p>
I think I was actually referring to the casting of incompatible pointers part. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Fri, 02 Jul 2010 07:43:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m pretty sure a determined programmer will find a creative way around that, too.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Fri, 02 Jul 2010 13:44:14 +0000</pubDate>
	</item>
</rss>
