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

时间:2011-08-21 09:13:49

标签: delphi

我收到编译错误'Ambiguous overloaded call to ...'没有明显的原因。这seems to be a bug in 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.

2 个答案:

答案 0 :(得分:5)

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

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

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

答案 1 :(得分:4)

这是一个编译器错误,它已在D2009中修复。