delphi - 为什么 Str() 将 "W1057 Implicit string cast from ' ShortString' 给 'string' "?

标签 delphi unicode delphi-xe2

考虑:

function x_StrZero(N: Double; W: Integer; D: Integer = 0): String;
var S : String;
begin
  Str(N:W:D,S);   
  S := Trim(S);

这给出了 W1057 隐式字符串从 'ShortString' 到 'string' 的转换

online doc说:

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).

为什么会这样?

我想阻止这种丑陋的解决方法:

function x_StrZero(N: Double; W: Integer; D: Integer = 0): String;
var
  S : String;
  SS : ShortString;
begin
  Str(N:W:D,SS);
  S := Trim(String(SS));

我已阅读 Why does Delphi warn when assigning ShortString to string?但这并不能回答这个问题。

最佳答案

Str(N:W:D,S);   

编译为

S := System._Str2Ext(N, W, D);

其中 System._Str2Ext 是一个返回类型为 ShortString 的函数。它在分配给 S 时被转换为 string。该警告虽然不容易阅读,但却是正确的,此时存在隐式转换。因此,要么通过避免 Str 来重写代码,使那里没有隐式转换,要么关闭警告,要么忽略警告。

关于delphi - 为什么 Str() 将 "W1057 Implicit string cast from ' ShortString' 给 'string' "?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12493021/

相关文章:

delphi - 如何释放 Delphi Prism 中对象的数组列表?

c++ - 如何将 char* 传递给 GetModuleHandle 函数?

unicode - FPDF utf-8 编码(操作方法)

c++ - 将 UTF-8 std::string 解码为 std::u32string?

delphi - 如何有条件地将属性保存到DFM或不保存?

multithreading - 在 TThread.Execute 中调用 TDataModule 方法

delphi - 面板显示在网格滚动条下

c++ - 浮点运算的编程风格

delphi - 如何从Windows获取用于存储缓存缩略图的永久目录?

delphi - IDE编译成功,但dcc32写入: Error: E2010 Incompatible types: 'Integer' and 'NativeInt'