Switch off Delphi range checking for a small porti

2019-01-23 16:14发布

How can one switch off range checking for a part of a file. Switching off is easy, but how do I revert to the project setting later on? The pseudo-code below should explain it:

Unit1;

//here's range checking on or off as per the project setting

code here...

{$R-}

//range checking is off here because the code causes range check errors

code here...

//now I want to revert to the project setting. How do I do that?

code here...

end.

2条回答
何必那么认真
2楼-- · 2019-01-23 16:31

See: IFOPT directive.

{$IFOPT R+}
  {$DEFINE RANGEON}
  {$R-}
{$ELSE}
  {$UNDEF RANGEON}
{$ENDIF}
//range checking is off here because the code causes range check errors
//code here...
{$IFDEF RANGEON}
  {$R+}
  {$UNDEF RANGEON}
{$ENDIF}
查看更多
【Aperson】
3楼-- · 2019-01-23 16:46

Wrap your code in $R directives:

{$R-} // disable range checking
// do non-range-checked operations here
{$R+} // turn range checking back on

Note that the directive applies at the statement level. You cannot wrap just part of an expression with that.

查看更多
登录 后发表回答