www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Do I need to use static here?

reply Machine Code <jckj33 gmail.com> writes:
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?

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.
Jan 08 2019
parent Steven Schveighoffer <schveiguy gmail.com> writes:
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
Jan 08 2019