模糊重载调用'foo'.D6中的错误?bug的范围?

soi*_*oid 1 delphi

我收到一个编译错误'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)

Dav*_*nan 5

我不确定这是一个bug还是设计但我认为你会在尝试基于不同整数类型的重载时遇到麻烦.例如,编译器无法区分整数文字,例如1是整数还是int64或字节或单词或shortint等.它可以是任何一种.虽然编译器可以制定规则来区分,但我认为它们不会直观.

在您的示例中,当您传递整数变量时,可以调用任一例程.

我更喜欢将重载保持在最低限度,只需要一个int64版本即可.

  • 但我不相信它可能是设计的,因为如果从函数和调用中删除了数组参数,它就会编译好.如果是设计的话,这也应该给出错误. (2认同)