在下面的代码中,记录构造函数做了一些奇怪的事情。
它在所有情况下都可以正常工作,除了下面标记的行:
program Project9;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Generics.Collections;
type
TIntegerPair = TPair<Integer, Integer>;
type
TMiniStack<T> = record
public
Items: array[0..10] of T;
SP: integer;
procedure Init;
procedure PushInline(const Item: T); inline;
procedure PushNormal(const Item: T);
end;
procedure TMiniStack<T>.Init;
begin
FillChar(Items, SizeOf(Items), #0);
SP:= 0;
end;
procedure TMiniStack<T>.PushInline(const Item: T);
begin
Items[SP]:= Item;
Inc(SP);
end;
procedure TMiniStack<T>.PushNormal(const Item: T);
begin
Items[SP]:= Item;
Inc(SP);
end;
procedure RecordConstructorFail;
var
List1: TMiniStack<TIntegerPair>;
List2: array[0..2] of TIntegerPair;
Pair: TIntegerPair;
a: string;
begin
Writeln('start test...');
FillChar(List1, SizeOf(List1), #0);
List1.Init;
List1.PushInline(TIntegerPair.Create(1, 1));
List1.PushInline(Pair.Create(2, 2)); <<--- Failure
List2[0]:= TIntegerPair.Create(1, 1);
List2[1]:= Pair.Create(2, 2);
if (List1.Items[0].Key <> 1) or (List1.Items[1].Key <> 2) then Writeln('something is wrong with List1-Inline');
if (List2[0].Key <> 1) or (List2[1].Key <> 2) then Writeln('something is wrong with List1');
List1.Init;
List1.PushNormal(TIntegerPair.Create(1, 1));
List1.PushNormal(Pair.Create(2, 2));
if (List1.Items[0].Key <> 1) or (List1.Items[1].Key <> 2) then Writeln('something is wrong with List1-Normal');
Writeln('Done');
Readln(a);
Writeln(a);
end;
begin
RecordConstructorFail;
end.
为什么这条线会导致失败?
List1.PushInline(Pair.Create(2, 2)); <<- Failure: Dumps the data somewhere else.
它是编译器错误吗?
还是我错过了什么?
我正在使用德尔福 XE6。