Can not get struct member addresses at compile time

Doeme doeme at the-internet.org
Wed Jun 16 15:47:34 UTC 2021


On Wednesday, 16 June 2021 at 13:36:07 UTC, Ali Çehreli wrote:
> On 6/16/21 2:27 AM, Doeme wrote:
>
> > How does one get the address of a struct member?
>
> Here is an experiment with offsetof and opDispatch:

Cool stuff!
I actually tried a very similar approach once, but it did not 
work out, since the D compilers refuse to do pointer arithmetic 
at compile time :/

```d
struct Foo{
  ubyte bar;
}

void* membaddr(void *ptr, ulong offset){
     return ptr+offset;
}

__gshared Foo foo;
void* bar = membaddr(&foo, foo.bar.offsetof);
//Error: cannot perform arithmetic on `void*` pointers at compile 
time
```

I guess that the opDispatch-method will suffer from the same 
issue...


More information about the Digitalmars-d-learn mailing list