[Issue 19667] New: .offsetof cannot be used on non-public members of aggregates in different modules

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Feb 11 04:43:10 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=19667

          Issue ID: 19667
           Summary: .offsetof cannot be used on non-public members of
                    aggregates in different modules
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: flyboynw at gmail.com

Consider the following code:

module mod1;
public struct S {
    public int publicField;
    private int privateField;
}

-----
module mod2;
import mod1;

void main() {
    import std.conv : to;
    import std.stdio : writeln;
    writeln(to!string(S.publicField.offsetof)); //OK
    writeln(to!string(S.privateField.offsetof)); //Compiler error
}

This means that is impossible to correctly map the memory layout a given
aggregate from outside of the implementing module using only .offsetof.

I am aware that .tupleof can be used to bypass the protection checks, but using
.tupleof is a hack. Why can .tupleof bypass the checks, but not .offsetof?

--


More information about the Digitalmars-d-bugs mailing list