unit SmartPointer; interface type Auto = reference to function: T; TAuto = class(TInterfacedObject, Auto) private FValue: T; public constructor Create(AValue: T); destructor Destroy; override; function Invoke: T; end; TAutoRelease = record private FValue: Auto; public class operator Implicit(AValue: TAutoRelease): T; inline; end; Auto = class public class function Wrap(AValue: T): TAutoRelease; inline; static; end; implementation { TAuto } constructor TAuto.Create(AValue: T); begin inherited Create; FValue := AValue; end; destructor TAuto.Destroy; begin FValue.Free; inherited; end; function TAuto.Invoke: T; begin Result := FValue; end; { TAutoRelease } class operator TAutoRelease.Implicit(AValue: TAutoRelease): T; begin Result := AValue.FValue(); end; { Auto } class function Auto.Wrap(AValue: T): TAutoRelease; begin Result.FValue := TAuto.Create(AValue); end; end.