An inline function is a function whose code gets inserted into the caller's code stream. Like a #define macro, inline functions improve performance by avoiding the overhead of the call itself and (especially!) by the compiler being able to optimize through the call ("procedural integration").
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
In straight C, you can achieve "encapsulated structs" by putting a void* in a struct, in which case the void* points to the real data that is unknown to users of the struct. Therefore users of the struct don't know how to interpret the stuff pointed to by the void*, but the access functions cast the void* to the approprate hidden type. This gives a form of encapsulation.
Unfortunately it forfeits type safety, and also imposes a function call to access even trivial fields of the struct (if you allowed direct access to the struct's fields, anyone and everyone would be able to get direct access since they would of necessity know how to interpret the stuff pointed to by the void*; this would make it difficult to change the underlying data structure).
Function call overhead is small, but can add up. C++ classes allow function calls to be expanded inline. This lets you have the safety of encapsulation along with the speed of direct access. Furthermore the parameter types of these inline functions are checked by the compiler, an improvement over C's #define macros.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
Because #define macros are evil in 4 different ways: evil#1, evil#2, evil#3, and evil#4.
Unlike #define macros, inline functions avoid infamous macro errors since inline functions always evaluate every argument exactly once. In other words, invoking an inline function is semantically just like invoking a regular function, only faster:
Also unlike macros, argument types are checked, and necessary conversions are performed correctly.
Macros are bad for your health; don't use them unless you have to.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
When you declare an inline function, it looks just like a normal function:
But when you define an inline function, you prepend the function's definition with the keyword inline, and you put the definition into a header file:
Note: It's imperative that the function's definition (the part between the {...}) be placed in a header file, unless the function is used only in a single .cpp file. In particular, if you put the inline function's definition into a .cpp file and you call it from some other .cpp file, you'll get an "unresolved external" error from the linker.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
When you declare an inline member function, it looks just like a normal member function:
But when you define an inline member function, you prepend the member function's definition with the keyword inline, and you put the definition into a header file:
It's usually imperative that the function's definition (the part between the {...}) be placed in a header file. If you put the inline function's definition into a .cpp file, and if it is called from some other .cpp file, you'll get an "unresolved external" error from the linker.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
Yep: define the member function in the class body itself:
Although this is easier on the person who writes the class, it's harder on all the readers since it mixes "what" a class does with "how" it does them. Because of this mixture, we normally prefer to define member functions outside the class body with the inline keyword. The insight that makes sense of this: in a reuse-oriented world, there will usually be many people who use your class, but there is only one person who builds it (yourself); therefore you should do things that favor the many rather than the few.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
Nope.
Beware that overuse of inline functions can cause code bloat, which can in turn have a negative performance impact in paging environments.
The term code bloat simply means that the size of the code gets larger (bloated). In the context of inline functions, the concern is that too many inline functions might increase the size of the executable (i.e., cause code bloat), and that might cause the operating system to thrash, which simply means it spends most of its time going out to disk to pull in the next chunk of code.
Of course it's also possible that inline functions will decrease the size of the executable. This may seem backwards, but it's really true. In particular, the amount of code necessary to call a function is sometimes greater than the amount of code to expand the function inline. This can happen with very short functions, and it can also happen with long functions when the optimizer is able to remove a lot of redundant code that is, when the optimizer is able to make the long function short.
So the message is this: there is no simple answer. You have to play with it to see what is best. Do not settle for a simplistic answer like, "Never use inline functions" or "Always use inline functions" or "Use inline functions if and only if the function is less than N lines of code." These one-size-fits-all rules may be easy to use, but they will produce sub-optimal results.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
E-mail the author
[ C++ FAQ Lite
| Table of contents
| Subject index
| About the author
| ©
| Download your own copy ]
Revised Aug 15, 2001