Artifact
3c0e4ea3648b71e883f42b6f2c60eb483175a76ba06e620f2a89d7b8b270b3f5:
Wiki page
[get the address of a field] by
admin
2021-10-08 15:52:03.
D 2021-10-08T15:52:03.878
L get\sthe\saddress\sof\sa\sfield
P e6efabfb2249290d1e00ddb141a5a121622f24646b48d2995a4ab8636f62fc36
U admin
W 1155
<h3>Getting the address of a field... when the field might be a property function</h3>
<div class="language-d"><verbatim>
struct S {
private int n;
ref int prop() return { return n; }
}
auto ref eval(T)(auto ref T t) { return t; }
/// How do we get a pointer to n?
unittest {
auto s = S(13);
auto p1 = &s.prop; // wrong
auto p2 = &(s.prop); // wrong
// auto p3 = &(x => x)(s.prop); // error
int* p4 = &eval(s.prop); // eval helper
assert(typeof(p1).stringof == "int delegate() ref return");
assert(typeof(p2).stringof == "int delegate() ref return");
assert(p2() == 13);
(*p4)++;
p4[0]++;
assert(p1() == 15);
// technically works, but requires this code to be in the same module as
// S's definition, and requires us to know the name of the real variable.
// what if S is a user-provided type?
int* p5 = &s.n;
// eval works for fields as well
int* p6 = &eval(s.n);
}
</verbatim></div>
[https://forum.dlang.org/post/sjkh5i$2p17$1@digitalmars.com|Discussion] (especially [https://forum.dlang.org/post/sjpofu$2bk$1@digitalmars.com|this post])
Z f94bb42352e46907b6e3e21ca0a52d81