enum

Benjamin Thaut code at benjamin-thaut.de
Wed Apr 9 08:44:10 PDT 2014


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.

Kind Regards
Benjamin Thaut



More information about the Digitalmars-d mailing list