Inline Functions
I've been reading about inline functions and their advantages, but my book doesn't cover the disadvantages, so what are they?
inlining larger functions can cause your exe to be larger in size then necessary, if programming for a device with low hardware requirements you might not want that. It could also slow a larger function because the function call itself may be faster if it were not inlined. So its best used for smaller functions containing one or two lines.
Increased code size, which can mean more memory use (both total and time spent accessing thing that have fallen out of cache). Generally, the bigger the function, the less likely it's a good idea to inline it. The opposite also holds true (that the smaller it is, the more likely your compiler will inline it for you on the assumption that it's probably a good idea).
Also take a look at Inline functions, C++ FAQ Lite, C++ FAQ Lite is probably one of the best C++ sites out there by the way [grin]
Yes, increased function size can really lead to thrashing. As a result most compilers have options that control the maximum size of a function that can be inlined. GCC has -fno-default-inline -fno-inline -finline-functions and -finline-limit=N. All the games I have worked on recently have shipped with -Os enabled (optimise for size). Visual C has equivalents that I cannot be bothered to fish out of the online browser-thrashing horror that is MSDN.
Also, not all functions can be inlined. This is true for recursive functions and dynamic dispatch functions.
Having inline functions in header files could increase compile time too. If you change the implementation of the inline function the client code will need to be recompiled, instead of just the function's source if the function weren't inlined.
Quote:Original post by rip-off
Having inline functions in header files could increase compile time too. If you change the implementation of the inline function the client code will need to be recompiled, instead of just the function's source if the function weren't inlined.
In a large library, that's definitely something that can cause suffering.
I just remembered a detail about binary compatibility. Inline functions in a dynamically linked library can break binary compatibility with a compiled application, since the function definition can be changed in the library, but the old code will be executed by the compiled application since there's no call. The drawback of that is that it might not be possible to update the library without recompiling the applications that use it.
There is drawback to inline functions that nobody mentioned. When you make a function inline, you must expose implementation details that would normally be hidden. Problems can include circular dependencies, private functions and data which must be made public, and longer compile times.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement