digitalmars.D.learn - Do I need to use static here?
- Machine Code (22/22) Jan 08 2019 I'm using enum to compute the value at runtime like this:
- Steven Schveighoffer (5/34) Jan 08 2019 No, enums are not part of the instance, they are part of the type. foo
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
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








Steven Schveighoffer <schveiguy gmail.com>