Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.


Artifact ID: e6efabfb2249290d1e00ddb141a5a121622f24646b48d2995a4ab8636f62fc36
Page Name:get the address of a field
Date: 2021-10-07 06:44:23
Original User: admin
Next 3c0e4ea3648b71e883f42b6f2c60eb483175a76ba06e620f2a89d7b8b270b3f5

Getting the address of a field... when the field might be a property function

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);


    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);