gpt4 book ai didi

delphi - 我可以强制 `const` 通过引用传递(又名丢失的 `in` 参数)

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

德尔福有:

var :通过引用传递;参数既是输入又是输出。
out :通过引用传递;参数仅输出。
const:经过......这取决于;参数仅输入。
in : 通过引用传递;参数只是输入,不会改变没有“in”。

我不介意 there is no spoon ,但我想念in;考虑以下代码,是否有更简洁的方法来执行此操作?

type TFastDiv = record 
strict private
FBuffer: Int64;
other fields
....

//Must be `var` because `const` would pass a Int64 by value
// |||
// VVV
function DivideFixedI32(var Buffer: Int64; x: integer): integer;
asm
mov r9,rcx
imul dword ptr[rcx] // do stuff with the buffer
..
mov ecx, [r9+4] // do other stuff with the rest of the buffer

{将代码更改为 imul ecx;...;shr r9,32;mov ecx,r9d 将允许按值传递,但我们假设代码不允许被改变。}

class operator TFastDiv.IntDivide(x:integer; const buffer:TFastDiv):integer;
begin
Result:= DivideFixedI32(Int64((@buffer.FBuffer)^), abs(x)); <<-- Ugly
if (x < 0) then Result:= - Result;
end;

DivideFixed 永远不会改变缓冲区。该例程的重点是 buffer 是一个预先计算的值,不会改变。

在类运算符中,我将 buffer 声明为 const,因为记录不得更改。

问题是:
如果我坚持将 IntDivide 中的 buffer 参数声明为 const 是否有更简洁的编码方式,或者我是否陷入了pointer_to/points_to hack ?

最佳答案

较新的编译器版本(从 XE3 开始)支持 [Ref] 装饰器:

procedure Foo(const [Ref] Arg1: Integer; [Ref] const Arg2: Byte);

示例改编自the documentation ,它强调 [Ref] 可以位于 const 关键字之前或之后。

关于delphi - 我可以强制 `const` 通过引用传递(又名丢失的 `in` 参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19094375/

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