Inline Functions

 

The reserved word inline used before a function’s return type instructs the compiler to insert a copy of that function into the program at each call to the function, rather than making the function call.

 

When to use

Use for very small often-used functions.

 

Advantage

Makes execution faster because the overhead of the function-call is avoided.

 

Disadvantage

Increases program size, memory footprint.

 

Dose of Reality

The compiler generally makes functions inline when it wishes, whether or not you asked it to.  “Inline is considered a hint to the compiler not a command.  That means compilers are free to ignore your inline directives whenever they want to, and it’s not hard to make them want to.” (Effective C++, Scott Myers, p.138)

 

Most likely ignored if your function:

  1. ever have a pointer to it.
  2. contains loops.
  3. is recursive.
  4. is virtual.