klu*_*udg 13 delphi type-promotion
请考虑以下代码:
program Promote;
{$APPTYPE CONSOLE}
uses
SysUtils;
{$HINTS OFF}
type
MyWord = record
FValue: LongWord;
class operator Implicit(AValue: LongWord): MyWord;
end;
class operator MyWord.Implicit(AValue: LongWord): MyWord;
begin
Result.FValue:= AValue;
end;
procedure Test(AValue: MyWord); overload;
begin
Writeln('MyWord');
end;
procedure Test(AValue: UInt64); overload;
begin
Writeln('UInt64');
end;
var
LW: LongWord;
begin
Test(LW);
Readln;
end.
Run Code Online (Sandbox Code Playgroud)
当我运行它(Delphi XE)时,我看到编译器将LongWord参数提升为UInt64(内置类型),而不是MyWord(用户定义的类型).
我是否可以假设Delphi编译器总是将内置类型提升为内置类型(如果在编译器本身中实现了此类提升)?
更一般地说,在这种情况下控制类型促销的规则是什么(比如我们有2个内置类型,或2个用户定义类型等)?
据我了解,序数类型 likeuint64总是比 a 更接近于另一种序数类型longword,例如record, 。时期。
“更近的类型”距离与“内置”或“自定义”类型无关。您正在比较苹果和橙子。
在编译器中,记录和序数是两个不同的家族。事实上,您可以定义隐式转换,但绝不会促使 arecord成为序数类型。
因此,“内置”与新序数类型string的亲和力总是比integer新序数类型少,例如:
type TMyInteger64 = type Int46;
Run Code Online (Sandbox Code Playgroud)
在这里,这种TMyInteger64类型将比内置string类型“更接近”。
一旦定义了 a record,它就会与其他类型(而不是序数类型)有亲缘关系record。
| 归档时间: |
|
| 查看次数: |
311 次 |
| 最近记录: |