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; Auto = class public class function Wrap(AValue: T): Auto; {inline;} static; // MEMO: inlineを付けると挙動が変わる(nilの代入で解放されない) 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; { Auto } class function Auto.Wrap(AValue: T): Auto; begin Result := TAuto.Create(AValue); end; end.