Inline Functions

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. Instead of transferring control to and from the function code segment, a modified copy of the function body may be substituted directly for the function call. In this way, the performance overhead of a function call is avoided.

A function is declared inline by using the inline function specifier or by defining a member function within a class or structure definition. The inline specifier is only a suggestion to the compiler that an inline expansion can be performed; the compiler is free to ignore the suggestion.

The following code fragment shows an inline function definition.

inline int add(int i, int j) { return i + j; }

The use of the inline specifier does not change the meaning of the function. However, the inline expansion of a function may not preserve the order of evaluation of the actual arguments. Inline expansion also does not change the linkage of a function: the linkage is external by default.

C++In C++, both member and nonmember functions can be inlined. Member functions that are implemented inside the body of a class declaration are implicitly declared inline. Constructors, copy constructors, assignment operators, and destructors that are created by the compiler are also implicitly declared inline. An inline function that the compiler does not inline is treated similarly to an ordinary function: only a single copy of the function exists, regardless of the number of translation units in which it is defined.

C In C, any function with internal linkage can be inlined, but a function with external linkage is subject to restriction. The restrictions are as follows:

In C, an inline definition is distinct from the corresponding external definition and from any other corresponding inline definitions in other translation units.

When source code is compiled allowing language extensions, the behavior of inline functions follows the GNU C semantics. If a function definition has extern inline explicitly specified, the compiler uses the extern inline definition only for inlining. The behavior resembles macro expansion. If an extern inline definition of a function exists in a header file, an external definition for the function without extern or inline must be available from another file. This definition is used for calls to that function from files that do not include the header file.

The following example illustrates the semantics of extern inline. When compiled with the GNU semantics, a noninline function body is not generated for two().

inline.h:
   #include<stdio.h>
 
   extern inline void two(void){  // GNU C uses this definition only for inlining
      printf("From inline.h\n");
   }
 
main.c:
   #include "inline.h"
 
   int main(void){
      void (*pTwo)() = two;
      two();
      (*pTwo)();
   }
 
two.c:
   #include<stdio.h>
 
      void two(){
      printf("In two.c\n");
   }

The output below shows the results when the first function call to two has indeed been inlined.

Using the gcc semantics for the inline keyword:
   From inline.h
   In two.c
 

The compiler might still choose not to inline the extern inline function two, despite the presence of the inline function specifier.

Related References

IBM Copyright 2003