gpt4 book ai didi

string - XE7 中的 Str 生成奇怪的警告

转载 作者:行者123 更新时间:2023-12-03 14:48:45 25 4
gpt4 key购买 nike

为什么这段代码:

  w: word;
s: String;
begin
str(w, s);

在 XE7 中生成此警告:

[dcc32 Warning] Unit1.pas(76): W1057 Implicit string cast from 'ShortString' to 'string'

汤姆

最佳答案

System.Str是一个可以追溯到过去时代的内在函数。 documentation说的是:

procedure Str(const X [: Width [:Decimals]]; var S: String);

....

Notes: However, on using this procedure, the compiler may issue a warning: W1057 Implicit string cast from '%s' to '%s' (Delphi).

If a string with a predefined minimum length is not needed, try using the IntToStr function instead.

由于这是一个内在的,所以可能会发生一些额外的事情。在幕后,内部函数是通过调用 RTL 支持函数来实现的,该函数生成 ShortString 。然后编译器魔法将其变成 string 。并警告您隐式转换。编译器的魔法转变

Str(w, s);

进入

s := _Str0Long(w);

哪里_Str0Long是:

function _Str0Long(val: Longint): _ShortStr;
begin
Result := _StrLong(val, 0);
end;

_Str0Long返回 ShortString那么编译器必须生成代码来执行 ShortString 的隐式转换至string当它分配给您的变量 s 时。当然,您很自然地会看到 W1057。

底线是 Str存在的唯一目的是保持与旧版 Pascal 的兼容性 ShortString代码。新代码不应调用 Str 。您应该按照文档的说明进行操作并调用 IntToStr :

s := IntToStr(w);

或者也许:

s := w.ToString;

关于string - XE7 中的 Str 生成奇怪的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28245449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com