Can not get struct member addresses at compile time
Ali Çehreli
acehreli at yahoo.com
Wed Jun 16 13:36:07 UTC 2021
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
More information about the Digitalmars-d-learn
mailing list