gpt4 book ai didi

delphi - 在其他哪些 Delphi 版本中,Cardinals 的 for 循环失败了?

转载 作者:行者123 更新时间:2023-12-03 18:17:05 29 4
gpt4 key购买 nike

请帮助我进行回归测试,并确定以下代码在哪些 Delphi 版本中失败。

编辑: 我知道有一个编译器提示;事实上,在我继承的一个项目中发现它以某种方式关闭了编译器提示(我看到很多人这样做,因为他们认为编译器提示总是无害的,这个案例表明它不是)。
出于文档目的,我仍然想知道在哪些 Delphi 版本中存在此编译器异常。

[DCC Hint] QC90921_SO4717399TestCase.pas(47): H2135 FOR or WHILE loop executes zero times - deleted

在 Delphi XE、2009、2007 和 5 中,它失败并显示以下输出。
我还没有时间研究其他 Delphi 版本。
请帮助我解决这个问题,并回答它在哪些其他 Delphi 版本上也失败了。

Low/High const fail: 0
Low/High hex literal fail: 0
Low/High decimal literal fail: 0

这段代码也是 QC 90921 的一部分。 :

program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;

var
CardinalIndex: Cardinal;
CardinalFirst: Cardinal;
CardinalLast: Cardinal;
Count: Int64;
Target: Int64;
begin
try
Target := High(Cardinal);
Inc(Target);

Count := 0;
for CardinalIndex := Low(CardinalIndex) to High(CardinalIndex) do
Inc(Count);
if Target <> Count then
Writeln('Low/High const fail: ', Count);

Count := 0;
for CardinalIndex := 0 to $FFFFFFFF do
Inc(Count);
if Target <> Count then
Writeln('Low/High hex literal fail: ', Count);

Count := 0;
for CardinalIndex := 0 to 4294967295 do
Inc(Count);
if Target <> Count then
Writeln('Low/High decimal literal fail: ', Count);

Count := 0;
CardinalFirst := Low(Cardinal);
CardinalLast := High(Cardinal);
for CardinalIndex := CardinalFirst to CardinalLast do
Inc(Count);
if Target <> Count then
Writeln('Low/High variable fail: ', Count);

Count := 0;
CardinalFirst := 0;
CardinalLast := $FFFFFFFF;
for CardinalIndex := CardinalFirst to CardinalLast do
Inc(Count);
if Target <> Count then
Writeln('hex literal Variable fail: ', Count);

Count := 0;
CardinalFirst := 0;
CardinalLast := 4294967295;
for CardinalIndex := CardinalFirst to CardinalLast do
Inc(Count);
if Target <> Count then
Writeln('decimal literal Variable fail: ', Count);

Write('Press <Enter>');
Readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.

编辑: 答案摘要;它在这些 Delphi 版本中失败:

  • 5
  • 6
  • 7
  • 2006
  • 2007
  • 2009
  • 2010

--杰伦

最佳答案

我想,因为这是一个记录在案的缺陷,所有 High(Cardinal) > High(Longint)(Delphi 4 及更高版本)的版本都会表现出这种行为。

来自“H2135: FOR or WHILE loop executes zero times - deleted (Delphi)”:

You may see this warning if a FOR loop increments its control variable from a value within the range of Longint to a value outside the range of Longint. For example:
var I: Cardinal;
begin
For I := 0 to $FFFFFFFF do
...
This results from a limitation in the compiler which you can work around by replacing the FOR loop with a WHILE loop.

关于delphi - 在其他哪些 Delphi 版本中,Cardinals 的 for 循环失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4717399/

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