need help to translate C into D

Ali Çehreli acehreli at yahoo.com
Tue Sep 13 23:34:33 UTC 2022


On 9/13/22 04:07, test123 wrote:
 > On Tuesday, 13 September 2022 at 10:59:36 UTC, Dennis wrote:

 >> Side node, you can use `immutable` instead of `__gshared const`, it
 >> amounts to the same for global variables.
 >
 > because __enums_layout.ptr need to be part of other object, and this
 > const ptr cloud be included in multi objects.

There may be valid reasons not to use 'immutable' but you can still do 
what you describe. There is an 'immutable' array below and its .ptr is 
being stored as 'const' inside objects of another struct.

struct S {
     int i;
     string s;
     int[3] arr;
}

immutable S[] imm;

shared static this() {
     // Initializing immutable at program initialization time:
     imm ~= S(42, "hello", [1,2,3]);
     imm ~= S(7, "world", [4,5,6]);
}

struct Other {
     const S * ptr;
}

void main() {
     auto o = Other(imm.ptr);
}

That works because 'const' can point to 'immutable' (and mutable and const).

Ali



More information about the Digitalmars-d-learn mailing list