www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19667] New: .offsetof cannot be used on non-public members of


          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 puremagic.com
          Reporter: flyboynw 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?

Feb 10 2019