Search code examples
delphiconstantsoffsetrecord

Offset of record field as constant


I'm using a technique like the following to get offsets of record fields at runtime:

procedure DoSomethingWithVar(var AField);
DoSomethingWithVar(TGUID(nil^).D3); // TGUID and D3 for exposition only :-)

This works fine. Now I want to have these offsets as consts but can't get it to work:

const
  cMyVarTyped: Pointer = @TGUID(nil^).D3;
  cMyVarUntyped = @TGUID(nil^).D3;
  cMyOffsetTyped: INT_PTR = INT_PTR(@TGUID(nil^).D3);
  cMyOffsetUntyped = INT_PTR(@TGUID(nil^).D3);

All of them yield "E2026 Constant expression expected". Any ideas?

FWIW: Wrapping the declaration in {$WRITEABLECONST ON}/{$WRITEABLECONST OFF} doesn't change the error.


Solution

  • It's not possible to get the compiler to make a constant expression containing a member offset.