There\'s two things about inlining:
inline
keyword will be ignored if the compiler determines that the function cannot be inlined.It depends on your environment and what you want to do, so it is really hard to say when inlining is preferrable.
This link has some interesting reading about inlining. And some sound advice (which pretty much boils down to: avoid doing it)
Yes, if you want to put a function in a header file, and include that file in several translation units. This is in fact the main purpose of inline
in C++.
The inline
keyword has two functions:
inline
'd symbol may be defined in multiple translation units (typically because it is defined in a header, that is included from multiple files). Normally, this would result in a linker error, but it is allowed when you use the inline
keyword.Read Herb Sutters comments on inline:
http://www.gotw.ca/gotw/033.htm
Inline is also useful if you want the ability to inline functions from a library. Only by putting the code for the function in the header file (which requires inline), is the compiler able to inline the function. Of course it is still up the the compiler whether to inline the function or not.
There's a side effect of inline
keyword when you are building shared library. Inlined functions are not exported into symbol table nor into library's binary. As a result inline
keyword is crucial in aspect of shared libraries, since compiler won't be able to inline exported function. On the other hand library's inline function will be always inlined because it doesn't exist in the binary form of the library.