Do I need to use static here?

Steven Schveighoffer schveiguy at gmail.com
Tue Jan 8 18:54:51 UTC 2019


On 1/8/19 1:35 PM, Machine Code wrote:
> I'm using enum to compute the value at runtime like this:
> 
> struct A
> {
>      enum foo = A("foo", 10);
>      enum baa = A("baa", 20);
> 
>      string name;
>      int value;
>      alias value this;
> }
> 
> In order to avoid foo having its value (even if literal) copied every 
> time A instancied and duplicates, shall I use static here?

No, enums are not part of the instance, they are part of the type. foo 
and baa will only exist when used.

> I'm assuing that it works like this:
> 
> int f() {
>     int a = 30;
>     int b = 50;
> }
> 
> and static would make it like this:
> 
> int f() {
>    static int a = 20;
>    static int b = 30;
> }
> 
> int are examples, of course. I have A and array of A in my code.

static would also work, but is not necessarily usable at compile time.

-Steve


More information about the Digitalmars-d-learn mailing list