gpt4 book ai didi

delphi - 从语言 POV 来看,Succ/Prev 应该适用于指针吗?

转载 作者:行者123 更新时间:2023-12-02 04:29:15 25 4
gpt4 key购买 nike

我想知道 Succ/Prev 内在函数是否应该能够在类型指针类型上使用。就像 Inc/Dec和数学( PointerVar+1PointerVar-1 )。

这些仅将 succ/pred 应用于未列出的点的“序数类型”。 Pascal Report 1972 也是如此(称为标量类型)

但是http://www.gnu-pascal.de/gpc/Succ.html#Succ声明“Succ 对指针的应用是在 Borland Pascal 中定义的。”在指针数学之后排除这些函数似乎不合理。

这个限制是在语言方面得到证实的还是只是一个实现问题,因为 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

最佳答案

当然,没有。这是违反语言规则的。以下是正在实现的正式合约 SuccPred(来自 ISO/IEC 7185:1990):

succ(x)

From the expression x that shall be of an ordinal-type, this function shall return a result that shall be of the same type as that of the expression (see 6.7.1). The function shall yield a value whose ordinal number is one greater than that of the expression x, if such a value exists. It shall be an error if such a value does not exist.

pred(x)

From the expression x that shall be of an ordinal-type, this function shall return a result that shall be of the same type as that of the expression (see 6.7.1). The function shall yield a value whose ordinal number is one less than that of the expression x, if such a value exists. It shall be an error if such a value does not exist.

如您所见,SuccPred 仅为序数类型的参数定义,因此它与指针类型不兼容(由于缺乏固有的序数,根据契约(Contract))。

关于delphi - 从语言 POV 来看,Succ/Prev 应该适用于指针吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40008611/

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