【问题标题】:class operator with result of Extended type does not work in BDS2006?扩展类型结果的类运算符在 BDS2006 中不起作用?
【发布时间】:2023-03-17 18:46:01
【问题描述】:

如果我尝试编译以下代码,我会在最后一行 (E := R) 收到错误消息“E2010 Incompatible types: 'Extended' and 'TMyRec'”:

type
  TMyRec = record
    class operator Implicit(Rec: TMyRec) : Integer;
    class operator Implicit(Rec: TMyRec) : Extended;
  end;

class operator TMyRec.Implicit(Rec: TMyRec) : Integer;
begin
  Result := 1;
end;

class operator TMyRec.Implicit(Rec: TMyRec) : Extended;
begin
  Result := 1;
end;

var
  R : TMyRec;
  B : Byte;
  E : Extended;

begin
  B := R; //this is OK
  E := R; //E2010 Incompatible types: 'Extended' and 'TMyRec'
end.

我请朋友尝试在XE中编译它 - 编译成功。那么这是BDS2006中的一个错误吗?有没有办法解决这个问题?

【问题讨论】:

    标签: delphi operator-overloading delphi-2006


    【解决方案1】:

    这确实是一个编译器缺陷。修复它的唯一方法是升级到可以解决该缺陷的编译器版本。如果你不能这样做,你只需要以不同的方式编写代码。

    FWIW,在我看来,使用Extended 总是错误的。这种类型是非标准的,只存在于 x86 上。在将来的某个时候,如果您迁移到 x64,您会发现 Extended 映射到 Double。此外,类型的对齐会导致内存访问性能不佳,并且将数据存储为Extended 而不是Double 的程序往往会明显变慢。

    【讨论】:

    • 也感谢您提供有关Extended 的额外信息。无论如何,在这种情况下,确切的类型是无关紧要的,因为编译器在选择任何真实类型时都会失败。
    猜你喜欢
    • 2018-02-19
    • 2023-03-25
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    相关资源
    最近更新 更多