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:

struct Foo{
 ubyte bar;
 int i;
}

auto addrOf(T)(ref T t) {
  static struct AddrOf {
    void * origin;
    auto opDispatch(string member)() {
      return origin + mixin ("T." ~ member ~ ".offsetof");
    }
  }
  return AddrOf(&t);
}

import std.stdio;

__gshared Foo foo;

void main() {
  writeln(&foo);

  writeln(foo.addrOf.bar);
  writeln(foo.addrOf.i);

  // Alternative syntax
  writeln(addrOf(foo).bar);
  writeln(addrOf(foo).i);
}

Ali

Reply via email to