gpt4 book ai didi

delphi - Delphi如何解析带整型参数的重载函数?

转载 作者:行者123 更新时间:2023-12-03 14:34:55 25 4
gpt4 key购买 nike

考虑以下程序:

program IntegerOverloads;
{$APPTYPE CONSOLE}

procedure WordOrCardinal(Value: Word); overload;
begin
Writeln('Word');
end;

procedure WordOrCardinal(Value: Cardinal); overload;
begin
Writeln('Cardinal');
end;

procedure SmallintOrInteger(Value: Smallint); overload;
begin
Writeln('Smallint');
end;

procedure SmallintOrInteger(Value: Integer); overload;
begin
Writeln('Integer');
end;

procedure ShortintOrSmallint(Value: Shortint); overload;
begin
Writeln('Shortint');
end;

procedure ShortintOrSmallint(Value: Smallint); overload;
begin
Writeln('Smallint');
end;

procedure Main;
var
_integer: Integer;
_cardinal: Cardinal;
_word: Word;
begin
WordOrCardinal(_Integer);
SmallintOrInteger(_cardinal);
ShortintOrSmallint(_word);
end;

begin
Main;
Readln;
end.

XE2编译时的输出为:

Cardinal
Integer
Smallint

Delphi 6 编译时的输出是:

Word
Smallint
Shortint

documentation状态(强调我的):

You can pass to an overloaded routine parameters that are not identical in type with those in any of the routine's declarations, but that are assignment-compatible with the parameters in more than one declaration. This happens most frequently when a routine is overloaded with different integer types or different real types - for example:

procedure Store(X: Longint); overload;
procedure Store(X: Shortint); overload;

In these cases, when it is possible to do so without ambiguity, the compiler invokes the routine whose parameters are of the type with the smallest range that accommodates the actual parameters in the call.

但这似乎确实适用于此。示例代码中的过程调用都不接受容纳调用中实际参数的类型。

我找不到任何描述编译器遵循的规则的文档。谁能指点我这样的文档?

此问题是由以下文章提出的:

<小时/>

更新

根据 Ken White 评论的提示,我编写了另一个程序来说明更多奇怪之处:<​​/p>

program IntegerOverloadsPart2;
{$APPTYPE CONSOLE}

procedure Test(Value: Byte); overload;
begin
Writeln('Byte');
end;

procedure Test(Value: Word); overload;
begin
Writeln('Word');
end;

procedure Test(Value: Cardinal); overload;
begin
Writeln('Cardinal');
end;

procedure Test(Value: Uint64); overload;
begin
Writeln('Uint64');
end;

procedure Main;
var
_byte: Byte;
_shortint: Shortint;
_word: Word;
_smallint: Smallint;
_cardinal: Cardinal;
_integer: Integer;
_uint64: UInt64;
_int64: Int64;
begin
Writeln('Unsigned variables passed as parameters:');
Test(_byte);
Test(_word);
Test(_cardinal);
Test(_uint64);
Writeln;
Writeln('Signed variables passed as parameters:');
Test(_shortint);
Test(_smallint);
Test(_integer);
Test(_int64);
end;

begin
Main;
Readln;
end.

当由 XE2 编译时,输出为:

Unsigned variables passed as parameters:
Byte
Word
Cardinal
Uint64

Signed variables passed as parameters:
Uint64
Uint64
Uint64
Uint64

在 Delphi 6 上,我必须删除 UInt64 重载,因为 Delphi 6 上不存在该类型,输出为:

Unsigned variables passed as parameters:
Byte
Word
Cardinal

Signed variables passed as parameters:
Byte
Byte
Byte

同样,这两种行为看起来都不符合以下说法:

In these cases, when it is possible to do so without ambiguity, the compiler invokes the routine whose parameters are of the type with the smallest range that accommodates the actual parameters in the call.

最佳答案

无符号类型都不能容纳有符号类型;引用的文档与您的示例一致 - 它只是没有说明编译器将如何处理它们。另一方面,有符号类型可以容纳无符号类型(SmallInt 容纳 Byte,LongInt 容纳 Word,Int64 容纳 Cardinal):

program IntegerOverloadsPart3;
{$APPTYPE CONSOLE}

procedure Test(Value: ShortInt); overload;
begin
Writeln('Short');
end;

procedure Test(Value: SmallInt); overload;
begin
Writeln('Small');
end;

procedure Test(Value: LongInt); overload;
begin
Writeln('Long');
end;

procedure Test(Value: Int64); overload;
begin
Writeln('64');
end;

procedure Main;
var
_byte: Byte;
_word: Word;
_cardinal: Cardinal;
_uint64: UInt64;
begin
Writeln('Unsigned variables passed as parameters:');
Test(_byte);
Test(_word);
Test(_cardinal);
Test(_uint64);
Writeln;
end;

begin
Main;
Readln;
end.

Delphi XE 输出:

Small
Long
64
64

关于delphi - Delphi如何解析带整型参数的重载函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12526786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com