我想知道 Succ/Prev 内在函数是否应该能够用于类型化指针类型。就像Inc
/Dec
和数学(PointerVar+1
和PointerVar-1
)一样。
- http://docwiki.embarcadero.com/Libraries/en/System.Succ
- http://www.freepascal.org/docs-html/rtl/system/succ.html
这些仅适用于 succ/pred 未列出其中的点的“序数类型”。Pascal Report 1972 也是如此(称为标量类型)
但是http://www.gnu-pascal.de/gpc/Succ.html#Succ声称“在 Borland Pascal 中定义了 Succ 对指针的应用”。在 Pointers Math 之后排除这些函数似乎是不合理的。
这种限制是在语言方面得到证实还是只是一个实现问题,看到 Succ/Pred 函数被视为有些神秘?
program Project9; // Delphi does have reverse-analogu for Pos/PosEx functions
{$APPTYPE CONSOLE} // So Delphi IDE ( Version Insight) to cut away a last part
uses // of string abuses mixing of record helper (LastIndexOf)
System.SysUtils; // and System.Copy function. Searchinf to fix it found this...
var
OutPut, RemoteName: string;
P: PChar;
begin
try
OutPut := 'aaaaaa/zzzzzz';
P := StrRScan( PChar(OutPut), '/');
P := Succ(P);
// XE2: [DCC Fatal Error] Project9.dpr(13): F2084 Internal Error: AV0C068241-R00000000-0
// 10.1: [dcc32 Error] Project9.dpr(13): E2008 Incompatible types
P := 1+P; // Another way to say Succ() - and works in both XE2 and 10.1
Inc(P); // Yet one more way to say Succ() - and works in both XE2 and 10.1 too
RemoteName := P;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
将它与更改的 var 类型进行比较很有趣 -P: Pointer;
而不是 PChar。
var P: Pointer; S: String;
P := Succ(P); // error
Inc(P); // error
P := 1+P; // works in XE2 if {$POINTERMATH ON}, error if {$POINTERMATH OFF}
// error in 10.1 regardless
S := PChar(P); // crashes XE2 if "P := 1+P;" is there above