不能内联函数?
Can't inline a function?
我声明了几个函数只是为了便于阅读,我想检查一下编译器是否内联了它们。根据 this answer,我想我可以将它们标记为 inline
并在它们未内联时得到提示,但是,当我尝试这样做时,出现以下错误:
[dcc32 Error] MyClass.pas(266): E1030 Invalid compiler directive: 'INLINE'
所以我尝试了一个简单的功能:
procedure TMyClass.Swap(var a, b : Integer); inline;
var
c : Integer;
begin
c := a;
a := b;
b := c;
end;
唉,我得到了同样的错误。根据 the docs 默认值是 {$INLINE ON}
,所以我假设我只需要添加 inline;
。尽管如此,我尝试声明 {$INLINE ON}
,但无济于事。我的 Google-fu 失败了,所以我来了。
我正在使用 Delphi 10.1 Berlin。
您将其放在实现中,而不是声明中。将其放在实现中将适用于独立的函数和过程,但不适用于 class 方法。这些必须在声明本身中定义为 inline
。
interface
type
TMyClass = class(TObject)
private
procedure Swap(var a, b: integer); inline;
end;
implementation
procedure TMyClass.Swap(var a, b:integer);
begin
//
end;
我声明了几个函数只是为了便于阅读,我想检查一下编译器是否内联了它们。根据 this answer,我想我可以将它们标记为 inline
并在它们未内联时得到提示,但是,当我尝试这样做时,出现以下错误:
[dcc32 Error] MyClass.pas(266): E1030 Invalid compiler directive: 'INLINE'
所以我尝试了一个简单的功能:
procedure TMyClass.Swap(var a, b : Integer); inline;
var
c : Integer;
begin
c := a;
a := b;
b := c;
end;
唉,我得到了同样的错误。根据 the docs 默认值是 {$INLINE ON}
,所以我假设我只需要添加 inline;
。尽管如此,我尝试声明 {$INLINE ON}
,但无济于事。我的 Google-fu 失败了,所以我来了。
我正在使用 Delphi 10.1 Berlin。
您将其放在实现中,而不是声明中。将其放在实现中将适用于独立的函数和过程,但不适用于 class 方法。这些必须在声明本身中定义为 inline
。
interface
type
TMyClass = class(TObject)
private
procedure Swap(var a, b: integer); inline;
end;
implementation
procedure TMyClass.Swap(var a, b:integer);
begin
//
end;