Why are multiple instances of the single enum created?

Mike Parker aldacron at gmail.com
Mon Feb 1 10:24:59 UTC 2021


On Monday, 1 February 2021 at 10:08:21 UTC, Per Nordlöw wrote:

> Agreed. My question is why does an `enum` require an extra 
> instance from a compiler architecture point of view? Do all 
> compilers handle compile-time enum instances in that way?

There is no "extra" instance because there's no single instance 
of an enum value. They have no address. When you use one, it's 
just as if you were writing the literal instead of the enum name. 
In other words:

enum ea = [1, 2, 3];
auto a0 = ea;
auto a1 = ea;

is identical to this:

auto a0 = [1, 2, 3];
auto a1 = [1, 2, 3];

It's purely a compile-time construct. Essentially just an alias 
for a literal.




More information about the Digitalmars-d-learn mailing list