Skip to content
Search

C++:inline

lastmod 2018-08-17created 2015-08-2210 edits

An inline function is a function, in several versions of the C and C++ programming languages, for which the compiler is requested to perform inline expansion. In other words, instead of generating code that calls the function where it is defined, the programmer can request that the compiler insert the complete function body at every place the function is called.

Restrictions

A summary of the restrictions on inline functions.

  • Loop statements (do whie, while, for), switch, and goto cannot be used inside an inline function.
  • When calling an inline function, the inline function must be defined before it is called.
  • Recursive calls cannot be made inside an inline function.
  • An inline function cannot be called more than once within a single expression.
  • The address of an inline function cannot be taken with a function pointer.
    This is because an inline function works by substitution and expansion, not by calling.

Tip

static/inline keyword position

Should keywords such as static or inline go on the Prorotype or the Implementation?

Answer: the Prototype.

Assembly result

Compares the Assembly output of the code below with and without the inline (__attribute__((always_inline))) syntax.

__attribute__((always_inline)) int add(int i)
{
    return i + i;
}

int main()
{
    int a = add(10);
    return 0;
}

For reference, the output was extracted using objdump.

Normal

With inline

test.o:     file format mach-o-x86-64

Disassembly of section .text:

0000000000000000 <__Z3addi>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   89 7d fc                mov    %edi,-0x4(%rbp)
   7:   8b 7d fc                mov    -0x4(%rbp),%edi
   a:   03 7d fc                add    -0x4(%rbp),%edi
   d:   89 f8                   mov    %edi,%eax
   f:   5d                      pop    %rbp
  10:   c3                      retq   
  11:   66 66 66 66 66 66 2e    data16 data16 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
  18:   0f 1f 84 00 00 00 00 
  1f:   00 

0000000000000020 <_main>:
  20:   55                      push   %rbp
  21:   48 89 e5                mov    %rsp,%rbp
  24:   48 83 ec 10             sub    $0x10,%rsp
  28:   bf 0a 00 00 00          mov    $0xa,%edi
  2d:   c7 45 fc 00 00 00 00    movl   $0x0,-0x4(%rbp)
  34:   e8 00 00 00 00          callq  39 <_main+0x19>
  39:   31 ff                   xor    %edi,%edi
  3b:   89 45 f8                mov    %eax,-0x8(%rbp)
  3e:   89 f8                   mov    %edi,%eax
  40:   48 83 c4 10             add    $0x10,%rsp
  44:   5d                      pop    %rbp
  45:   c3                      retq   
test2.o:     file format mach-o-x86-64

Disassembly of section .text:

0000000000000000 <__Z3addi>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   89 7d fc                mov    %edi,-0x4(%rbp)
   7:   8b 7d fc                mov    -0x4(%rbp),%edi
   a:   03 7d fc                add    -0x4(%rbp),%edi
   d:   89 f8                   mov    %edi,%eax
   f:   5d                      pop    %rbp
  10:   c3                      retq   
  11:   66 66 66 66 66 66 2e    data16 data16 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
  18:   0f 1f 84 00 00 00 00 
  1f:   00 

0000000000000020 <_main>:
  20:   55                      push   %rbp
  21:   48 89 e5                mov    %rsp,%rbp
  24:   31 c0                   xor    %eax,%eax
  26:   c7 45 f8 00 00 00 00    movl   $0x0,-0x8(%rbp)
  2d:   c7 45 fc 0a 00 00 00    movl   $0xa,-0x4(%rbp)
  34:   8b 4d fc                mov    -0x4(%rbp),%ecx
  37:   03 4d fc                add    -0x4(%rbp),%ecx
  3a:   89 4d f4                mov    %ecx,-0xc(%rbp)
  3d:   5d                      pop    %rbp
  3e:   c3                      retq   

As the result shows, with inline the callq instruction is gone and the add instruction is included.

Favorite site

References


  1. Netrance.blog.naver.com_-inline_function-_asm.pdf ↩