Ambiguous overloaded call to 'foo'. Bug in D6? Scope of bug?
I am getting a compile error 'Ambiguous overloaded call to ...' for no apparent reason. This seems to be a bug in Delphi 6. What is the scope of this bug? Does it only affect functions with 'array of' parameters? Has it been fixed in newer versions of 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.
I'm not sure if it's a bug or by design but I think you will get into trouble trying to overload based on different integer types. For example, the compiler can't distinguish between integer literals, e.g. is 1 an integer or an int64 or a byte or a word or a shortint etc. It could be any of those. Whilst the compiler can make rules to distinguish, I don't think they would be intuitive.
In your example, when you pass an integer variable, either routine could be called.
I prefer to keep overloading to a minimum which you can do here by having just an int64 version.
This is a compiler bug and it has been fixed in D2009.
精彩评论