Delphi允许Copy
函数的3个版本:
function CopyTest(const S: string): string;
begin
Result:= Copy(S, 1, 5);
Result:= Copy(S, 1);
// Result:= Copy(S); // not allowed for strings, allowed for dyn arrays
end;
FreePascal似乎只编译第一个(3-arg)版本;对于其他人我有编译时错误
Error: Wrong number of parameters specified for call to "$fpc_ansistr_copy"
我是否遗漏了一些FPC编译器开关或Free Pascal中没有Copy
重载?
答案 0 :(得分:6)
'copy'节点生成器代码位于pinline.pas
FPC源的inline_copy
函数中。仅对于动态数组,变体1和3是有效的(在变体3的情况下,它生成将第二和第三参数传递给fpc_dynarray_copy
的代码)。对于所有其他情况(ansi字符串,宽字符串,unicode字符串,char(*)和短字符串),需要3个参数(编译器生成对其中一个复制函数的调用(例如fpc_ansistr_copy
中的astrings.pas
)没有检查参数,因为被调用的函数没有重载或默认参数,所以需要完全匹配参数)。没有涉及开关/指令。
(*)这个有点奇怪,它返回一个短裤本身或''。
答案 1 :(得分:4)
由于我知道Free Pascal支持默认值参数,因此不需要重载功能。您可以写新的Copy
函数,例如......
function Copy(const S: string; From: integer = 1; Count: integer = MaxInt): string;
begin
//There is no need to check the string length
// if Count > Length(S) then
// Count := Length(S);
result := system.Copy(S, From, Count);
end;