Choosing Which Functions to Inline

The compiler offers several methods to specify which functions are eligible for inlining.

To specify that a function is eligible to be inlined, precede its definition with the inline, __inline__, or __inline keyword. To allow these keywords in C source code, turn off ANSI Keywords Only in the CodeWarrior IDE's C/C++ Language settings panel or turn off the only_std_keywords pragma in your source code.

To verify that an eligible function has been inlined or not, use the Non-Inlined Functions option in the IDE's C/C++ Warnings panel or the warn_notinlined pragma. The following listing shows an example:

Listing: Specifying to the compiler that a function may be inlined

#pragma only_std_keywords off
inline int attempt_to_inline(void)

{

    return 10;

}

To specify that a function must never be inlined, follow its definition's specifier with __attribute__((never_inline)). The following listing shows an example.

Listing: Specifying to the compiler that a function must never be inlined

int never_inline(void) __attribute__((never_inline))
{

   return 20;

}

To specify that no functions in a file may be inlined, including those that are defined with the inline, __inline__, or __inline keywords, use the dont_inline pragma. The following listing shows an example:

Listing: Specifying that no functions may be inlined

#pragma dont_inline on
/* Will not be inlined. */

inline int attempt_to_inline(void)

{

    return 10;

}

/* Will not be inlined. */

int never_inline(void) __attribute__((never_inline))

{

   return 20;

}

#pragma dont_inline off

/* Will be inlined, if possible. */

inline int also_attempt_to_inline(void)

{

    return 10;

}

The kind of functions that are never inlined are as follows:

The compiler will not inline these functions, even if they are defined with the inline, __inline__, or __inline keywords.

The following functions also should never be inlined:

It can inline such functions if the class has a trivial empty constructor as in this case:

  struct X {
      int n;
      X(int a) { n = a; }
      ~X() {}
  };
  inline X f(X x) { return X(x.n + 1); }
  int main()
  {
      return f(X(1)).n;
  }  

This does not depend on "ISO C++ templates".