发现:d2006的inline不是很可靠

alphax alphax 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
今天做一个函数发现一些怪事,通过调试发现,delphi2006的inline新语法在特定情况下会出现错误。我做了个测试程序,不知道有没有搞错,大家鉴定鉴定,如果真的有问题,大家以后使用时要小心哟

///////////////////////////
// project
///////////////////////////

program inline_err;

{$APPTYPE CONSOLE}

uses
SysUtils,
other_unit in 'other_unit.pas';

procedure normal(var k: my_type; delta_x, delta_y: integer);
//没有inline的函数
begin
inc(k.x1, delta_x);
inc(k.x2, delta_x);

inc(k.y1, delta_y);
inc(k.y2, delta_y);
end;

procedure compare(normal_r, inlined_r: my_type);
//比较结果
begin
if (normal_r.x1 <> inlined_r.x1)
or (normal_r.x2 <> inlined_r.x2)
or (normal_r.y1 <> inlined_r.y1)
or (normal_r.y2 <> inlined_r.y2) then
writeln('Error')
else
writeln('Ok');
end;

procedure test_failed();
var
source, normal_r, inlined_r: my_type;
begin
source.x1 := 10;
source.x2 := 20;
source.y1 := 10;
source.y2 := 20;

normal_r := source;
inlined_r := source;

normal(normal_r, -normal_r.x1, -normal_r.y1);
inlined(inlined_r, -inlined_r.x1, -inlined_r.y1);

compare(normal_r, inlined_r);
end;

procedure test_failed2();
var
source, normal_r, inlined_r: my_type;
begin
source.x1 := 10;
source.x2 := 20;
source.y1 := 10;
source.y2 := 20;

normal_r := source;
inlined_r := source;

normal(normal_r, -normal_r.x1, -normal_r.y1);
inlined2(inlined_r, -inlined_r.x1, -inlined_r.y1);

compare(normal_r, inlined_r);
end;

procedure test_success();
var
source, normal_r, inlined_r: my_type;
begin
source.x1 := 10;
source.x2 := 20;
source.y1 := 10;
source.y2 := 20;

normal_r := source;
inlined_r := source;

normal(normal_r, -normal_r.x2, -normal_r.y2);
inlined(inlined_r, -inlined_r.x2, -inlined_r.y2);

compare(normal_r, inlined_r);
end;



var
s: string;
begin
{ TODO -oUser -cConsole Main : Insert code here }
test_failed();
test_failed2();
test_success();

writeln('press ENTRY to continue..');
readln(s);
end.


/////////////////////////////
/// other unit
/////////////////////////////
unit other_unit;

interface
uses
SysUtils, Types;

type
my_type = record //or object
x1, y1, x2, y2: integer;
end;

procedure inlined(var k: my_type; delta_x, delta_y: integer); inline;
procedure inlined2(var k: my_type; delta_x, delta_y: integer); inline;

implementation

procedure inlined(var k: my_type; delta_x, delta_y: integer);
begin
inc(k.x1, delta_x);
inc(k.x2, delta_x);

inc(k.y1, delta_y);
inc(k.y2, delta_y);
end;

procedure inlined2(var k: my_type; delta_x, delta_y: integer);
begin
k.x1 := k.x1 + delta_x;
k.x2 := k.x2 + delta_x;

k.y1 := k.y1 + delta_y;
k.y2 := k.y2 + delta_y;
end;


end.

运行可以发现,inline版的函数没有正确被编译,产生这种情况的条件:
1. inline函数在别的单元
2. 调用时,k.x1,k.y1作为delta_x, delta_y的实际参数
...全文

相关推荐

详解CSS中的display:flex||inline-flex属性|56375.pdf

介绍 Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。任何一个容器都可以指定为Flex布局。 flex: 将对象作为弹性伸缩盒显示 inline-flex:将对象作为内联块级弹性伸缩盒显示 flex示例代码 &lt;!DOCTYPE html> &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;title&gt;Test&lt;/title&gt; &lt;style type="text/css"&gt; .main{ width:200px; background-color: red

weixin_38689041

rollup-plugin-inline-js:使用inline-js进行汇总

rollu-plugin-inline-js 使用。 如果您使用汇总构建源并希望在构建时内联一些静态资产,这可能会很有用。 安装 npm install -D rollup-plugin-inline-js 用法 import inline from "rollup-plugin-inline-js" ; export default { input : "entry.js" , output : { file : "dist/output.js" , format : "es" } , plugins : [ inline ( ) ] } ; 该插件将替换源代码中的$inline指令,就像。 配置 .inline.js配置文件按预期工作。 原料药 该模块导出单个功能。 createPlugin const plugin = createPlug

梦想是世界和平

CSS解决inline-block的错位问题

废话不多说 帖代码

weixin_38540819

posthtml-inline-favicon:PostHTML插件到内嵌图标

posthtml-inline-favicon posthtml-inline-favicon是用于posthtml-inline-favicon的插件。 前: &lt; head &gt; &lt; link rel =" shortcut icon " href =" favicon.ico " /&gt; &lt;/ head &gt; 后: &lt; head &gt; &lt; link rel =" icon " type =" image/png " href =" [removed]... " /&gt; &lt;/ head &gt; 安装 yarn add -D posthtml-inline-favicon # OR npm i posthtml-inline-favicon 用法 const fs = require ( 'fs' ) ; const posthtml = require (

卡卡乐乐

菲尼克斯电气Inline IO产品选型速查表.pdf

菲尼克斯电气Inline IO产品选型速查表pdf,菲尼克斯电气Inline IO产品选型速查表

weixin_38744270
评论 3
收藏 已收藏
分享
返回顶部