Can not get struct member addresses at compile time

Ali Çehreli acehreli at yahoo.com
Wed Jun 16 21:42:41 UTC 2021


On 6/16/21 8:47 AM, Doeme wrote:

 > 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...

No, opDispatch does not work either for compile time addresses.

Actually, it is news to me that the compiler can know (determine?) the 
address of a global variable. I thought the loador would determine the 
addresses, but apparently not. Is it really a constant compiled value in 
the case of C? Can you show an example please?

Ali




More information about the Digitalmars-d-learn mailing list