我收到一个编译错误'Ambiguous overloaded call to ...'没有明显的原因.这似乎是Delphi 6中的一个错误.这个bug的范围是什么?它只影响'数组'参数的函数吗?它是否已在较新版本的Delphi中修复?
// This is a simple example to produce the compiler error, not a real program.
function ShowInt(const a: array of string; const i: longint): string; overload;
begin
Result := 'Longint ' + IntToStr(i);
end;
function ShowInt(const a: array of string; const i: int64): string; overload;
begin
Result := 'Int64 ' + IntToStr(i);
end;
procedure Test;
var
i64: int64;
ilong: longint;
begin
ShowInt([], i64 ); // In D6 why does this line compile OK, but next line gives
ShowInt([], ilong); // compile error: Ambiguous overloaded call to 'ShowInt'.
end; // And if the array parameter is removed it compiles OK.
Run Code Online (Sandbox Code Playgroud)
我不确定这是一个bug还是设计但我认为你会在尝试基于不同整数类型的重载时遇到麻烦.例如,编译器无法区分整数文字,例如1是整数还是int64或字节或单词或shortint等.它可以是任何一种.虽然编译器可以制定规则来区分,但我认为它们不会直观.
在您的示例中,当您传递整数变量时,可以调用任一例程.
我更喜欢将重载保持在最低限度,只需要一个int64版本即可.