enum

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Apr 9 09:47:22 PDT 2014


On 4/9/14, 8:44 AM, Benjamin Thaut wrote:
> Am 08.04.2014 21:08, schrieb Andrei Alexandrescu:
>> (moving http://goo.gl/ZISWwN to this group)
>>
>> On 4/7/14, 3:41 PM, w0rp wrote:
>>> Yeah, I've seen this happen before. I think we could actually introduce
>>> a little more type safety on enums without a great deal of breakage. It
>>> would be nice to have a final switch give you as much of a guarantee
>>> about what it's doing as it can.
>>
>> People use enums for things such as:
>>
>> 1. A discrete set of categorical values:
>>
>> enum State { initial, waiting, running, done }
>>
>> That's probably the most common use, and the one that prompted the
>> moniker "enum(eration)".
>>
>
> This seems to be exactly the use case for which C++ enum class has been
> designed. But there is one major flaw in C++ enum class. Usually if you
> have a discrete set you want to use it to index into an array.
>
> E.g.
>
> enum ShaderType { Vertex, Geometry, Pixel };
> Shader[ShaderType.max] shaders;
> shaders[ShaderType.Vertex] = new Shader();
>
> In C++ you can't index arrays using a enum class without casting them
> first. This is in my opinion a major problem. We should not make the
> same mistake, in case we redesign enums.

Very true. In hhvm, we tried an enum class to avoid bugs with using 
wrong indices in a couple of specific arrays. There were so many darned 
casts around, we had to revert the change.

Andrei



More information about the Digitalmars-d mailing list