Request assistance initializing struct instance at global scope

user1234 user1234 at 12.fr
Mon Dec 7 05:43:51 UTC 2020


On Monday, 7 December 2020 at 05:28:41 UTC, user1234 wrote:
> On Monday, 7 December 2020 at 04:13:16 UTC, Andrew Edwards 
> wrote:
>> Given:
>>
>> ===============
>> extern(C):
>> char*[] hldr;
>> enum I = (1<<0);
>> struct S { char* ft; char** fm; int f; }
>>
>> void main(){}
>> ===============
>>
>> How do I initialize an instance of S at global scope?
>
> You cant. At the global scope the initializers must be runnable 
> at compile time, i.e using CTFE. I've tried to simplify what 
> would be required:
>
> ---
> extern(C):
> char*[] hldr;
> enum I = (1<<0);
> struct S { char* ft; char** fm; int f; }
>
> void main(){}
>
> enum char[8] FirstLevel  = ['0'];
> enum         DoubleLevel = &FirstLevel[0]; // here
>
> S[] s = [
>     S(
>         FirstLevel.ptr,
>         DoubleLevel,          // error is here actually, 
> interesting
>         0),
> ];
> ---
>
> D is not able of that :
>
> /tmp/temp_7F835402B0F0.d:9:28: Error: cannot use non-constant 
> CTFE pointer in an initializer `&['0', '\xff', '\xff', '\xff', 
> '\xff', '\xff', '\xff', '\xff'][0]`

sorry my example was bad, better one

---
extern(C):
char*[] hldr;
enum I = (1<<0);
struct S { char* ft; char** fm; int f; }

void main(){}

enum char[8] Stuff      = ['0'];
enum         FirstLevel = Stuff.ptr; // &Suffn so char* OK

S[] s = [
     S(
         FirstLevel,                           // char* OK
         &FirstLevel, // error here               char** NG at CTFE
         0),
];
---


More information about the Digitalmars-d-learn mailing list