繁体   English   中英

在Delphi XE2中使用泛型和转发声明时的编译器错误

[英]Compiler bug when using generics and forward declaration in Delphi XE2

我开始使用Delphi 2010项目,然后迁移到XE,现在我尝试迁移到XE2。 在XE2(Update 4 Hotfix 1)中进行编译后,单元测试开始失败。 经过一些调试后,很明显以下代码未正确编译:

program ForwardDeclaration;

{$APPTYPE CONSOLE}

uses
    System.SysUtils;

type
    TEntityBase = class(TObject)
    protected
        FModel: Integer;
    public
        constructor Create(const AModel: Integer);
    end;

    TEntity<TKey> = class(TEntityBase)
    end;

    TMyEntity2 = class;

    TMyEntity1 = class(TEntity<Integer>)
        FData: Integer;
    end;

    TMyEntity2 = class(TMyEntity1)
    end;

constructor TEntityBase.Create(const AModel: Integer);
begin
    inherited Create;
    FModel := AModel;
end;

var
    MyEntity: TMyEntity1;
begin
    try
        Writeln(TEntityBase.ClassName, ': ', TEntityBase.InstanceSize, ' bytes');
        Writeln(TMyEntity1.ClassName, ': ', TMyEntity1.InstanceSize, ' bytes');
        MyEntity := TMyEntity1.Create(100);
        Assert(MyEntity.FData = 0);
    except
        on E: Exception do Writeln(E.ClassName, ': ', E.Message);
    end;
end.

方案产出:

TEntityBase: 12 bytes
TMyEntity1: 12 bytes <-- Must be 16 bytes!
EAssertionFailed: Assertion failure (ForwardDeclaration.dpr, line 41)

是否可以通过调整编译器选项来解决问题?

这个问题是否会在其他人身上重复出现?

PS QC107110

是否可以通过调整编译器选项来解决问题?

,你无法通过调整来修复错误,这是编译器中的一个(非常具体的)错误。

[有人可以告诉我]这个问题是否会在其他人身上重复出现?

我可以重现代码,但只能在XE2 update 4中重现。

我无法在XE3中检查它(没有那个版本)。 它在XE4中修复(根据评论)。

因此,让代码工作的唯一方法是:

一个。 删除不需要的前向声明。
使用不同版本的Delphi。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM