Can not get struct member addresses at compile time

Doeme doeme at the-internet.org
Wed Jun 16 09:27:25 UTC 2021


Hi!

I'm currently investigating why I can not take the address of a 
static struct-element at compile time (afaik the linker should be 
able to resolve this, and doing the identical thing in C works...)

```d
struct Foo{
  ubyte bar;
}

__gshared Foo foo;
void* baz = \&foo;       //works
void* bar = \&foo.bar;   //Error: static variable `foo` cannot be 
read at compile time
```

Does this have to do with ".bar" potentially being a function 
call?
How does one get the address of a struct member?

The higher level problem of this question is that I want to make 
a pointer-map of a struct:

```d
import std.meta;
struct Foo{
     ubyte a;
     ushort b;
}

template ptrize(alias S){
	enum ptrize =  \&S;
}

__gshared Foo f;
static immutable void*[] map = [staticMap!(ptrize, f.tupleof)];
```

Is there an alternative to get to this point? Static module 
initializers are not really an option, since the whole thing 
should be -betterC.

Thanks for any hints!


More information about the Digitalmars-d-learn mailing list