Inline Functions

To avoid the overhead associated with a function call (creation of a stack frame containing copies of arguments or addresses of reference parameters and the return address) C++ permits you to declare functions to be inline. Such a declaration is a request to the compiler that it replace each call to the function with the fully expanded code of the function. For example:

inline int max(int a, int b){
 return a > b ? a : b ;
}

int main(){
 int temp = max(3,5);
 etc....
}

The compiler could substitute the expanded code for max as shown here.

int main() {
 int temp;
 {
 int a = 3;
 int b = 5;
 temp = a > b ? a : b;
 }
 etc.......
}

The inlining of a function can give a significant boost in performance if it is called repeatedly (e.g., inside a large loop). The penalty for inlining a function is that it might make the compiled code larger, which will cause the program to use more memory while it is running. For small functions that get called many times, that memory effect will be small while the potential performance gain might be large.

There are no simple answers to the question of whether inlining will improve the performance of your program or not. A lot depends on the optimization settings of the compiler. A lot depends on the nature of the program. Does it make very heavy use of the processor? Does it make heavy use of system memory? Does it spend a lot of its time interacting with slow devices (e.g., input and output)? The answers to these questions affect the answer to the question of whether or not to inline, and we leave them to a more advanced treatment of the subject. For an overview of the complexity of this issue, visit Marshall Cline's FAQ Lite site.[2]

[2] http://snet.wit.ie/GreenSpirit/c++-faq-lite/inline-functions.html

An inline function is similar to a #define macro with one very important difference: The substitution process for a #define macro is handled by the preprocessor, which is essentially a text editor. The substitution process for an inline function is handled by the compiler, which will perform the operation much more intelligently with proper type checking. We discuss this in more detail in the next section.

Some Rules about Inline Functions

  • An inline function must be defined before it is called (a declaration is not enough).
  • An inline definition can only occur once in any source code module.
  • If a class member function's definition appears inside the class definition, the function is implicitly inline.

If a function is too complex, or the compiler options are switched, the compiler may ignore the inline directive. Most compilers refuse to inline functions that contain:

  • while, for, do . . . while statements
  • switch statements
  • More than a certain number of lines of code

If the compiler does refuse to inline a function, it treats it as a normal function and generates regular function calls.



Part I: Introduction to C++ and Qt 4

C++ Introduction

Classes

Introduction to Qt

Lists

Functions

Inheritance and Polymorphism

Part II: Higher-Level Programming

Libraries

Introduction to Design Patterns

QObject

Generics and Containers

Qt GUI Widgets

Concurrency

Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments

Bibliography

MP3 Jukebox Assignments



An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268

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