Section 7.6. Inline Functions


7.6. Inline Functions

Recall the function we wrote on page 248 that returned a reference to the shorter of its two string parameters:

      // find longer of two strings      const string &shorterString(const string &s1, const string &s2)      {          return s1.size() < s2.size() ? s1 : s2;      } 

The benefits of defining a function for such a small operation include:

  • It is easier to read and understand a call to shorterString than it would be to read and interpret an expression that used the equivalent conditional expression in place of the function call.

  • If a change needs to be made, it is easier to change the function than to find and change every occurrence of the equivalent expression.

  • Using a function ensures uniform behavior. Each test is guaranteed to be implemented in the same manner.

  • The function can be reused rather than rewritten for other applications.

There is, however, one potential drawback to making shorterString a function: Calling a function is slower than evaluating the equivalent expression. On most machines, a function call does a lot of work: registers are saved before the call and restored after the return; the arguments are copied; and the program branches to a new location.

inline Functions Avoid Function Call Overhead

A function specified as inline (usually) is expanded "in line" at each point in the program in which it is invoked. Assuming we made shorterString an inline function, then this call

          cout << shorterString(s1, s2) << endl; 

would be expanded during compilation into something like

          cout << (s1.size() < s2.size() ? s1 : s2)               << endl; 

The run-time overhead of making shorterString a function is thus removed.

We can define shorterString as an inline function by specifying the keyword inline before the function's return type:

      // inline version: find longer of two strings      inline const string &      shorterString(const string &s1, const string &s2)      {              return s1.size() < s2.size() ? s1 : s2;      } 

The inline specification is only a request to the compiler. The compiler may choose to ignore this request.



In general, the inline mechanism is meant to optimize small, straight-line functions that are called frequently. Many compilers will not inline a recursive function. A 1,200-line function is also not likely to be explanded inline.

Put inline Functions in Header Files

Unlike other function definitions, inlines should be defined in header files.



To expand the code of an inline function at the point of call, the compiler must have access to the function definition. The function prototype is insufficient.

An inline function may be defined more than once in a program as long as the definition appears only once in a given source file and the definition is exactly the same in each source file. By putting inline functions in headers, we ensure that the same definition is used whenever the function is called and that the compiler has the function definition available at the point of call.

Whenever an inline function is added to or changed in a header file, every source file that uses that header must be recompiled.



Exercises Section 7.6

Exercise 7.29:

Which one of the following declarations and definitions would you put in a header? In a program text file? Explain why.

      (a) inline bool eq(const BigInt&, const BigInt&) {...}      (b) void putValues(int *arr, int size); 

Exercise 7.30:

Rewrite the is Shorter function from page 235 as an inline function.




C++ Primer
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2006
Pages: 223
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net