Jump to content
dummzeuch

Where do I declare a function inline ?

Recommended Posts

Given a unit that exports a function that should be inlined:

unit bla;

{$INCLUDE 'jedi.inc'}

interface

function blub: integer;

implementation

function blub: integer;
{$IFDEF SUPPORTS_INLINE}
inline;
{$ENDIF}
begin
  Result := SomeCalculationHere;
end;

Is it OK to put the inline only in the implementation? Or does it also have to go to the declaration in the interface section? It compiles OK this way, but I don't know whether it would still be inlined.

 

I'm asking because I would like to keep the interface as clean as possible which means avoiding to clutter it with an ifdef.

Edited by dummzeuch

Share this post


Link to post

I generally put these kind of functions in a record to group them and only put the inline directive in the interface however I do place a {$IFNDEF DEBUG} around the inline directive as I’ve found that I could not step through the methods while debugging. I know you need to keep the GExperts code backwards compatible so the record group is just a nice to have thing.

Share this post


Link to post

As it stands, in your code it will only be inlined in the code in the same unit as the implementation, and only for calls in functions declared after the inline method implementation. 

 

You can check this yourself by inspecting the disassembly of calling code. 

  • Thanks 1

Share this post


Link to post
1 hour ago, David Hoyle said:

{$IFNDEF DEBUG} around the inline directive

With include files one can change the inline option and skip all those IFNDEFs, tested with Berlin and up.
In this case the include file includes a CustomInclude.inc, which is part of the project. This makes it simple to edit in the IDE as required.

 

/// <remarks>
///    NOTE:  You can override this in CustomInclude.inc, but it requires a Build
///           and won't update the Blue Dots
/// </remarks>
{$IFNDEF RELEASE}
  {-$INLINE OFF} // un-comment as needed
{$ENDIF RELEASE}

 

  • Like 2
  • Thanks 2

Share this post


Link to post
On 1/1/2020 at 3:31 PM, dummzeuch said:

I don't know whether it would still be inlined.

Just try to F7 into that function.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×